2011-10-31 100 views

回答

147

只是嘗試這個示例代碼: -

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()); 
} 
} 

我希望這幫助...

+0

它適用於10位長的值,但不能在6向下。小時的默認值爲4 .. – ralphgabb

+4

您應該使用**新的SimpleDateFormat(日期格式,Locale.US(或您的語言環境_))**而不是**新的SimpleDateFormat(dateFormat)**,因爲它會導致崩潰,因爲更改默認安卓語言 –

9

試試這個代碼也許會有幫助,修改它適合你的需要

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

您可以在毫秒構建java.util.Date。然後使用java.text.DateFormat將其轉換爲字符串。

60

毫秒值轉換爲Date實例,並將其傳遞給選定的格式化程序。

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
String dateString = formatter.format(new Date(dateInMillis))); 
6

我終於找到工作正常的代碼對我來說

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 
3
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); 

      } 
     }); 
    } 
} 
+0

我們可以通過使用時間單位..但它不工作正常...使用這..這將幫助你..它工作正常 –

27
public static String convertDate(String dateInMilliseconds,String dateFormat) { 
    return DateFormat.format(dateFormat, Long.parseLong(dateInMilliseconds)).toString(); 
} 

調用此函數

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

謝謝Mahmood,這個解決方案需要API級別1(我的項目降低到API 15),而其他答案需要API級別24(日期和/或日曆庫) –

+0

如果您是美國人,該怎麼辦? – behelit

+0

救了我一天。我嘗試了許多使用簡單日期格式的解決方案。但沒有使用它。你的這個解決方案對我很好。謝謝 –

9
DateFormat.getDateInstance().format(dateInMS); 
3

短而有效:

DateFormat.getDateTimeInstance().format(new Date(myMillisValue)) 
1
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(); 
} 
+0

使用這些方法可以將日期以字符串格式(如2016-08-18)或任何類型的字符串格式轉換爲DateFormat,並且還可以將日期轉換爲毫秒。 –

0
public static String toDateStr(long milliseconds, String format) 
{ 
    Date date = new Date(milliseconds); 
    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US); 
    return formatter.format(date); 
}