-1

I Have the following code in my application

System.out.println(rec.getDateTrami().getTime());

I need to convert the following format (I suppose that they are seconds)

43782000
29382000
29382000

To a format YYYY-MM-DD HH24:MI:SS, anyone can help to me?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
jc1992
  • 644
  • 3
  • 10
  • 24
  • Use the `SimpleDateFormat` https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html – CodeMatrix Dec 14 '17 at 12:06
  • Does `rec.getDateTrami()` return a `Date` object? Asking because the `Date` class is long outdated, and today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). If you can change `getDateTrami()` to return an instance of one of the modern classes, you should prefer that. – Ole V.V. Dec 14 '17 at 12:32
  • Please for your own sake (and ours too), search and research before asking. How to format a date-time in Java has been covered in so many places, both here on Stack Overflow and elsewhere. Only one word of warning when you go searching: avoid the solutions that mention `SimpleDateFormat`. Today that class is long outdated and notoriously troublesome. – Ole V.V. Dec 14 '17 at 12:37
  • @CodeMatrix `SimpleDateFormat` is exactly the class to avoid. And still more so if you are using Java 8, as your link seems to indicate. No matter the Java version, that class is troublesome. – Ole V.V. Dec 14 '17 at 12:40
  • jc1992, your numbers don’t really look like seconds in my eyes, are you sure? Have you got a picture of which date-times they should correspond to, just approximately, just within a couple of years? – Ole V.V. Dec 14 '17 at 12:50

3 Answers3

4

You can make use of the SimpleDateFormat

Example:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
date.setTime(rec.getDateTrami().getTime());
System.out.println(format.format(date));

Documentation: SimpleDateFormat, DateFormat

Pieter
  • 895
  • 11
  • 22
Jonas Michel
  • 68
  • 1
  • 8
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Even if you need to convert from an old-fashioned `Date` object first. – Ole V.V. Dec 14 '17 at 12:34
2

Use java.time

Best if you can change getDateTrami() to return an OffsetDateTime or ZonedDateTime from java.time. java.time is the modern Java date and time API. It is also known as JSR-310. The code is the same no matter which of the two mentioned types is returned:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    System.out.println(rec.getDateTrami().format(formatter));

This prints a date and time like

2017-12-14 16:52:20

java.time is generally so much nicer to work with than the outmoded Date class and its friends.

If you cannot change the return type

I assume getDateTrami() returns a java.util.Date. Since the Date class is long outmoded, the first thing to do is to convert it to java.time.Instant. From there you perform your further operations:

    Date oldfashionedDateObject = rec.getDateTrami();
    ZonedDateTime dateTime = oldfashionedDateObject.toInstant()
            .atZone(ZoneId.of("Atlantic/Cape_Verde"));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    System.out.println(dateTime.format(formatter));

The result is similar to the above, of course. I on purpose made explicit in which time zone I want to interpret the point in time. Please substitute your own if it doesn’t happen to be Atlantic/Cape_Verde.

Formatting seconds since the epoch

    int seconds = 29_382_000;
    ZonedDateTime dateTime = Instant.ofEpochSecond(seconds)
            .atZone(ZoneId.of("Atlantic/Cape_Verde"));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    System.out.println(dateTime.format(formatter));

This snippet prints

1970-12-06 23:40:00

A date in December 1970. If this is incorrect, it is because 29 382 000 didn’t denote seconds since the epoch of January 1, 1970 at midnight in UTC, also known as the Unix epoch. This is by far the most common time to measure seconds from. If your seconds are measured from some other fixed point in time, I cannot guess which, and you have got a job to do to find out. Again decide which time zone you want to specify.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You could use SimpledateFormat.

new SimpleDateFormat("YYYY-MM-DD HH24:MI:SS").format(date)
shalama
  • 1,133
  • 1
  • 11
  • 25