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)