Formatting a string timestamp with Android

后端 未结 1 345
时光取名叫无心
时光取名叫无心 2021-01-25 21:08

For some reason this has me tearing my hair out.

I have a UNIX timestamp as a string in Android. All I want to do is format this so that it returns the date/time in the

1条回答
  •  遥遥无期
    2021-01-25 21:44

    Use the SimpleDateFormat constructor with the Locale you need:

    http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat%28java.lang.String,%20java.util.Locale%29

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    Calendar c = Calendar.getInstance();
    try {
        Date dt = sdf.parse("2011-03-01 17:55:15"); 
        c.setTime(dt);
        System.out.println( c.getTimeInMillis());   
        System.out.println(dt.toString());   
    } catch (ParseException e) {
        System.err.println("There's an error in the Date!");
    }   
    

    outputs:

    1299002115000
    Tue Mar 01 12:55:15 EST 2011
    

    0 讨论(0)
提交回复
热议问题