2012-07-31 89 views
0

我有這樣的代碼:如何將毫秒內的字符串轉換爲日期?

String id = c.getString("data"); 
String name = ((TextView) view.findViewById(R.id.TextView05)).getText().toString(); 

public static String getDate(long seconds, String dateFormat) 
{ 
    DateFormat formatter = new SimpleDateFormat("yyyy MMMM dd HH:mm"); 
    long now = System.currentTimeMillis(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(now); 
    return formatter.format(calendar.getTime()); 
} 

"data"1341435600000。我想從毫秒到這個字符串。

+0

「數據」:1341435600000 – user1563977 2012-07-31 10:37:01

+1

有任何錯誤? – jeet 2012-07-31 10:39:54

+0

nope,剛剛得到134143560000000 – user1563977 2012-07-31 10:41:33

回答

0
try { 
     String str_date="11-June-07"; 
     DateFormat formatter ; 
     Date date ; 
     formatter = new SimpleDateFormat("dd-MMM-yy"); 
     date = (Date)formatter.parse(str_date); 
     System.out.println("Today is " +date); 
    } 
catch (ParseException e) 
{ 
System.out.println("Exception :"+e); 
} 

的轉換毫秒爲日期

long yourmilliseconds = 1119193190; 
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm"); 
Date resultdate = new Date(yourmilliseconds); 
0

事情是這樣的:

SimpleDateFormat ss = (SimpleDateFormat) SimpleDateFormat.getInstance(); 
    ss.applyPattern("yyyy MM dd HH:mm"); 

    // Date to String: 
    String dateToString = ss.format(new Date()); 

    //String to Date 
    Date stringToDate = ss.parse("2012 02 16 14:10")); 
0
String dtStart = "2010-10-15T09:27:37Z"; 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
try { 
    Date date = format.parse(dtStart); 
    System.out.println(date); 
} catch (ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
0

同樣的方法對我工作的罰款。爲什麼不是你的?

/** Called when the activity is first created. */ 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    long cTime = System.currentTimeMillis(); 

    String date = getDate(cTime, "dd/MM/yyyy hh:mm:ss.SSS"); 

    Toast.makeText(getApplicationContext(), date, Toast.LENGTH_SHORT).show(); 
} 

public static String getDate(long milliSeconds, String dateFormat) 
{ 
    // Create a DateFormatter object for displaying date in specified format. 
    DateFormat 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

String id = Utils.getDate(c.getString(「data」),「dd/MM/yyyy」);會是對的嗎? – user1563977 2012-07-31 11:11:32

+0

@ user1563977如果您的'data'包含時間毫秒的長時間值。那是對的。只要這樣做 - 'long cTime = c.getString(「data」);'把這個代碼,而不是'cTime'在上面的答案 – Praveenkumar 2012-07-31 11:18:55

+0

'String id = c.getString(TAG_DATA); \t \t \t \t long milliseconds = Long.parseLong(id); \t \t \t \t String date = Utils.getDate(毫秒,「yyyy-MM-dd HH:mm:ss」);'解決:)另一個問題,我有:)我應該寫什麼來知道什麼是GMT? – user1563977 2012-07-31 17:34:18

0

,如果你不打擾關於Date格式,只是用它下面

String id = c.getString("data"); //"1341435600000"; 
Date date123 = new Date(Long.parseLong(id)); 
System.out.println(date123); 
+0

我需要把map.put(TAG_ID,id); – user1563977 2012-07-31 11:08:38

+0

我沒有得到你。 (順便說一句,它沒有在你的原始問題中提到) – sunil 2012-07-31 11:10:52

0

的代碼片段,以幫助:

String timestamp="20140809"; 
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); 
System.out.println("date :"+dateFormat.parse(timestamp).getTime()); 
相關問題