Rendy Rendy - 1 month ago 26
Java Question

Date format for timestamp?

I have following Date String:


1477547160000+0800


Is there any date format for timestamp?

AFAIK, there is no date format to parse timestamp as in here

Answer

Assuming that the number before + is epoch milliseconds, and the number after + is timezone offset, you can try this:

String input = "1477547160000+0800";
String[] tokens = input.split("(\\+|-)");

long epoch = Long.parseLong(tokens[0]);
int offset = Integer.parseInt(tokens[1]);
int offsetHour = offset / 100;
int offsetMin = offset % 100;

OffsetDateTime t = Instant
    .ofEpochMilli(epoch)
    .atOffset(ZoneOffset.ofHoursMinutes(offsetHour, offsetMin));

Now you have OffsetDateTime instance, and you can convert it into String of any format you want. The default is:

t.toString() => "2016-10-27T13:46+08:00"

If you want specific format, you can use DateTimeFormatter.

t.format(new DateTimeFormatter(your_format_here));