日期格式化程序在j2me?

时间:2012-02-20 08:52:21

标签: date java-me time

我将输入设为milliseconds,我的输出为Sat Aug 13 14:00:00 GMT+05:30 2011

例如:

 Date resultdate = new Date(1313224200000L);
 System.out.println("Date: " + resultdate);

您如何将此日期格式化为13-08-2011,时间为14:00?

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法,

public static String getDate(long currentTimeMillis)
{
    Date today = new Date(currentTimeMillis);
    DateField date = new DateField( "", DateField.DATE_TIME, TimeZone.getTimeZone("GMT+5:30") );
    date.setDate(today);

    Calendar cal = Calendar.getInstance();
    cal.setTime(today);

    String dateStr = "" + getTwoDigitStr(cal.get(Calendar.DATE)) + "-" + getTwoDigitStr(cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.YEAR);

    return dateStr;
}

public static String getTime (long currentTimeMillis )
{
    Date today = new Date(currentTimeMillis);
    DateField date = new DateField ( "", DateField.TIME, TimeZone.getTimeZone("GMT +5:30")  );
    date.setDate(today);

    Calendar cal = Calendar.getInstance();
    cal.setTime(today);

    String timeStr = "" + cal.get( Calendar.HOUR) + ":" + cal.get( Calendar.MINUTE );
    return timeStr;
}