有没有办法在Java中将给定的Date
字符串转换为Milliseconds
(Epoch
长格式)?示例:我想转换
public static final String date = "04/28/2016";
毫秒(epoch)。
答案 0 :(得分:9)
Date.getTime() 返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
答案 1 :(得分:5)
您可以使用java.text.SimpleDateFormat简单地将其解析为java.util.Date并调用它的getTime()函数。它将返回自1970年1月1日以来的毫秒数。
public static final String strDate = "04/28/2016";
try {
Long millis = new SimpleDateFormat("MM/dd/yyyy").parse(strDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
答案 2 :(得分:1)
您可以创建一个 listMenuItems.setAdapter(null);
sa = new MenuListCustomAdapter(appContext, items, R.layout.listmenuitemlayout, from, to);
listMenuItems.setAdapter(sa);
//Custom listview adapter than handles complex items in the list.
private class MenuListCustomAdapter extends SimpleAdapter
{
private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
public MenuListCustomAdapter(Context context, List<HashMap<String, String>> objects, int resource, String[] from, int[] to)
{
super(context, objects, resource, from, to);
try
{
for(int i = 0; i < objects.size(); i++)
{
myChecked.put(i, false);
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e( clsGlobals.AppName, "Error in MenuActivity MenuListCustomAdapter: " + e.toString());
}
}
// handles the user checking items in the list.
public void toggleChecked(int position)
{
try
{
int calValue = targetCals;
if(myChecked.get(position))
{
myChecked.put(position, false);
caloriesSelected = caloriesSelected - Integer.parseInt(locatedMenuItems.get(position).get("Calories").toString());
carbsSelected = carbsSelected - Integer.parseInt(locatedMenuItems.get(position).get("Carbohydrates").toString());
}
else
{
myChecked.put(position, true);
caloriesSelected = caloriesSelected + Integer.parseInt(locatedMenuItems.get(position).get("Calories").toString());
carbsSelected = carbsSelected + Integer.parseInt(locatedMenuItems.get(position).get("Carbohydrates").toString());
}
calValue = calValue - caloriesSelected;
txtMsg.setText("Target Cals: " + targetCals + "\r\nCals. remaining: " + calValue + "\r\nSelected carbs: " + carbsSelected);
if (calValue < 0)
{
txtMsg.setTextColor(txtBoxRed);
}
else
{
txtMsg.setTextColor(txtBoxBlack);
}
notifyDataSetChanged();
}
catch (Exception e)
{
e.printStackTrace();
Log.e( clsGlobals.AppName, "Error in MenuActivity toggleChecked: " + e.toString());
}
}
public List<HashMap<String, String>> getCheckedItems()
{
List<HashMap<String, String>> checkedItems = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < myChecked.size(); i++)
{
if (myChecked.get(i))
{
(checkedItems).add(locatedMenuItems.get(i));
}
}
return checkedItems;
}
//Fills the items in the view. Gets called once per item being inserted in the list.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
try
{
row = convertView;
if(row==null)
{
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.listmenuitemlayout, parent, false);
}
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.lstDesc);
checkedTextView.setText(locatedMenuItems.get(position).get("MenuItemDesc").toString());
// TextView textView = (TextView)row.findViewById(R.id.lstName);
// textView.setText(locatedMenuItems.get(position).get("MenuItemName").toString());
TextView tvAttribs = (TextView)row.findViewById(R.id.lstAttribs);
String temp = " Cals: " + locatedMenuItems.get(position).get("Calories").toString() + ", Carbs: " + locatedMenuItems.get(position).get("Carbohydrates").toString();
temp+= ", Sodium: " + locatedMenuItems.get(position).get("Sodium").toString();
temp+= ", Fat: " + locatedMenuItems.get(position).get("Fat").toString();
temp+= ", Protein: " + locatedMenuItems.get(position).get("Protein").toString();
tvAttribs.setText(temp);
Boolean checked = myChecked.get(position);
if (checked != null)
{
checkedTextView.setChecked(checked);
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e( clsGlobals.AppName, "Error in MenuActivity toggleChecked: " + e.toString());
}
return row;
}
}
对象,然后将其日期设置为您想要的日期,然后调用其Calendar
方法。
getTimeInMillis()
如果您想将Calendar c = new Calendar.getInstance();
c.set(2016, 3, 28);
c.getTimeInMillis();
直接转换为日期,可以尝试:
String
答案 3 :(得分:0)
您需要使用Calendar实例从纪元
获取毫秒try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date d = sdf.parse("04/28/2016");
/*
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
*/
System.out.println(d.getTime());
//OR
Calendar cal = Calendar.getInstance();
cal.set(2016, 3, 28);
//the current time as UTC milliseconds from the epoch.
System.out.println(cal.getTimeInMillis());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}