-1

Easy in PHP but how will it be in Java.

$value="1562916792";
echo date("Y-m-d h:i:sa", $values['time']);

output: 2019-07-12 01:03:12pm

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Saquib Azam
  • 73
  • 1
  • 4
  • I guess it depends on where you get your *"numeric value."* What does the numeric value represent? – Zephyr Jul 13 '19 at 06:50
  • 1
    @Zephyr It's seconds since epoch (1/1/1970). – Andreas Jul 13 '19 at 07:17
  • In the linked question I recommend [the knowledgeable answer by Basil Bourque](https://stackoverflow.com/a/38547492/5772882). Only he assumes America/Chicago rather than America/Denver time zone, but I think you can change that. – Ole V.V. Jul 13 '19 at 07:25

1 Answers1

1

To get that time, I'll assume you are in India time zone, in which case you do it like this:

Using Java 8 Time API

int value = 1562916792;
System.out.println(Instant.ofEpochSecond(value)
                          .atZone(ZoneId.of("Asia/Kolkata")) // or ZoneId.systemDefault()
                          .format(DateTimeFormatter.ofPattern("uuuu-MM-dd hh:mm:ssa")));

Using old Java Date API

int value = 1562916792;
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ssa").format(value * 1000L));

Output

2019-07-12 01:03:12PM
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Agree, the desired output would agree with Sri Lanka or Inida time zone (Asia/Colombo or Asia/Kolkata). – Ole V.V. Jul 13 '19 at 07:28