convert millisecond to Joda Date Time or for zone 0000

31.1k views Asked by At

please tell me how to convert milliseconds to joda Date time??

formatter = DateTimeFormat.forPattern("dd/MM/yyyy'T'HH:mm:ss").withZone(DateTimeZone.forOffsetHoursMinutes(00, 00));

even tried

String millisecond="14235453511"
DateTime.parse(millisecond);
3

There are 3 answers

7
Meno Hochschild On BEST ANSWER

The answer given by @Adam S is almost okay. However, I would prefer to specify the timezone explicitly. Without specifying it you get the constructed DateTime-instance in the system timezone. But you want the zone "0000" (UTC)? Then look for this alternative constructor:

String milliseconds = "14235453511";
DateTime someDate = new DateTime(Long.valueOf(milliseconds), DateTimeZone.UTC);
System.out.println(someDate); // 1970-06-14T18:17:33.511Z
0
Adam S On

There's a constructor that takes milliseconds:

long milliseconds = 14235453511;
DateTime someDate = new DateTime(milliseconds);
1
Sandeep Singh On

You can use this

Calendar calendar = new GregorianCalendar();

        DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");

        formatter.setCalendar(calendar);
        String timeZone = "GMT+2:00";
        formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

        String time = formatter.format(calendar.getTime());

        System.out.println(time);

I hope this will help you