Search code examples
datejava-metime

date Formatter in j2me?


I'm giving the input as milliseconds and I'm getting output as Sat Aug 13 14:00:00 GMT+05:30 2011

Eg:

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

How will you format this date as 13-08-2011 and time as 14:00?


Solution

  • You can try following methods for same,

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