0

I need to convert the current timestamp and current minus 1-minute timestamp into a given format. I was able to convert the desired format. But it was returning as String.

I need the formatted output as Date instead of String.

public static void main(String[] args) throws ParseException
{
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
    long now = System.currentTimeMillis();
    String toTimeStamp = dateFormatter.format(now);
    long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
    String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
    System.out.println(fromTimeStamp);
    System.out.println(toTimeStamp);

    // Tried with parse
    Date fromDate = dateFormatter.parse(fromTimeStamp);
    Date toDate = dateFormatter.parse(toTimeStamp);

    System.out.println(fromDate);
    System.out.println(toDate);

}

Output:(String)

2020-07-24 12:13:00 
2020-07-24 12:12:00

Output: Pose Parsed the string as Date

Fri Jul 24 12:16:00 IST 2020
Fri Jul 24 12:17:00 IST 2020

Expected Output:

2020-07-24 12:13:00 (As Date Object)
2020-07-24 12:12:00 (As Date Object)
ramkumar-yoganathan
  • 1,918
  • 3
  • 13
  • 29
  • 3
    "I need the formatted output as Date instead of String" - This is not possible, you have a misunderstanding of what a `Date` object is. It's just a timestamp value, and it does not have a format. There is no such thing as "a `Date` object with a specific format". Just like an `int` is just a number, that doesn't contain any information about how the number should be formatted. If you need to display the `Date` object in a specific format, use a `SimpleDateFormatter` to format it accordingly. – Jesper Jul 24 '20 at 06:58
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 26 '20 at 09:03
  • 1
    Thank you very much. Today I Learned. – ramkumar-yoganathan Jul 26 '20 at 14:03

3 Answers3

2

Can't be possible. Understood lately. Closing this.

ramkumar-yoganathan
  • 1,918
  • 3
  • 13
  • 29
1

Once you transform your String into a Date, the initial format you used, whatever it is is lost.

If you want to display your data in the "yyyy-MM-dd HH:mm:00", use your formatter again otherwise Date class will stick to its default format:

System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));
Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18
1

If you need two Date-Object, you could do it like this:

    OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
    OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);

    System.out.println(Date.from(offsetDateTime1.toInstant()));
    System.out.println(Date.from(offsetDateTime2.toInstant()));

If you need the timestamp to be printed in other format, you can use SimpleDateFormat on the Date-Objects afterwards.

Seb
  • 1,721
  • 1
  • 17
  • 30