如何将毫秒转换为android的date格式?

我有毫秒。 我需要它被转换为date格式

例:

23/10/2011

如何实现它?

试试这个示例代码:

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class Test { /** * Main Method */ public static void main(String[] args) { System.out.println(getDate(82233213123L, "dd/MM/yyyy hh:mm:ss.SSS")); } /** * Return date in specified format. * @param milliSeconds Date in milliseconds * @param dateFormat Date format * @return String representing date in specified format */ public static String getDate(long milliSeconds, String dateFormat) { // Create a DateFormatter object for displaying date in specified format. SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); // Create a calendar object that will convert the date and time value in milliseconds to date. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(milliSeconds); return formatter.format(calendar.getTime()); } } 

我希望这个帮助…

毫秒值转换为Date实例并将其传递给选定的格式化程序。

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String dateString = formatter.format(new Date(dateInMillis))); 
 public static String convertDate(String dateInMilliseconds,String dateFormat) { return DateFormat.format(dateFormat, Long.parseLong(dateInMilliseconds)).toString(); } 

调用这个函数

 convertDate("82233213123","dd/MM/yyyy hh:mm:ss"); 

试试这个代码可能会帮助你,修改它适合你的需求

 SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); Date d = format.parse(fileDate); 
 DateFormat.getDateInstance().format(dateInMS); 

我终于find了适合我的正常代码

 Long longDate = Long.valueOf(date); Calendar cal = Calendar.getInstance(); int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis()); Date da = new Date(); da = new Date(longDate-(long)offset); cal.setTime(da); String time =cal.getTime().toLocaleString(); //this is full string time = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(da); //this is only time time = DateFormat.getDateInstance(DateFormat.MEDIUM).format(da); //this is only date 
 public class LogicconvertmillistotimeActivity extends Activity { /** Called when the activity is first created. */ EditText millisedit; Button millisbutton; TextView millistextview; long millislong; String millisstring; int millisec=0,sec=0,min=0,hour=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); millisedit=(EditText)findViewById(R.id.editText1); millisbutton=(Button)findViewById(R.id.button1); millistextview=(TextView)findViewById(R.id.textView1); millisbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { millisbutton.setClickable(false); millisec=0; sec=0; min=0; hour=0; millisstring=millisedit.getText().toString().trim(); millislong= Long.parseLong(millisstring); Calendar cal = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); if(millislong>1000){ sec=(int) (millislong/1000); millisec=(int)millislong%1000; if(sec>=60){ min=sec/60; sec=sec%60; } if(min>=60){ hour=min/60; min=min%60; } } else { millisec=(int)millislong; } cal.clear(); cal.set(Calendar.HOUR_OF_DAY,hour); cal.set(Calendar.MINUTE,min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND,millisec); String DateFormat = formatter.format(cal.getTime()); // DateFormat = ""; millistextview.setText(DateFormat); } }); } } 

简短有效:

 DateFormat.getDateTimeInstance().format(new Date(myMillisValue)) 
  public static Date getDateFromString(String date) { Date dt = null; if (date != null) { for (String sdf : supportedDateFormats) { try { dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime()); break; } catch (ParseException pe) { pe.printStackTrace(); } } } return dt; } public static Calendar getCalenderFromDate(Date date){ Calendar cal =Calendar.getInstance(); cal.setTime(date);return cal; } public static Calendar getCalenderFromString(String s_date){ Date date = getDateFromString(s_date); Calendar cal = getCalenderFromDate(date); return cal; } public static long getMiliSecondsFromString(String s_date){ Date date = getDateFromString(s_date); Calendar cal = getCalenderFromDate(date); return cal.getTimeInMillis(); } 
 public static String toDateStr(long milliseconds, String format) { Date date = new Date(milliseconds); SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US); return formatter.format(date); } 

您可以在毫秒级上构buildjava.util.Date。 然后使用java.text.DateFormat将其转换为string。