Skip to content

Instantly share code, notes, and snippets.

@geordee
Created April 19, 2021 09:26
Show Gist options
  • Save geordee/f4798d7653e658f1652ad6020757360a to your computer and use it in GitHub Desktop.
Save geordee/f4798d7653e658f1652ad6020757360a to your computer and use it in GitHub Desktop.
Timestamp Conversion and Comparison
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
class Main {
public static void main(String args[]) {
String documentDateTime = "2022-02-02T12:00:00+04:00";
String createDateTime = "2022-02-02T08:15:00.000Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss Z");
ZonedDateTime createDateTimeZ = ZonedDateTime.parse(createDateTime) ;
System.out.println(createDateTimeZ.format(formatter));
System.out.println(createDateTimeZ.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
ZonedDateTime documentDateTimeZ = ZonedDateTime.parse(documentDateTime);
System.out.println(documentDateTimeZ.format(formatter));
System.out.println(documentDateTimeZ.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
long differenceInMinutes = ChronoUnit.MINUTES.between(documentDateTimeZ, createDateTimeZ);
System.out.println(differenceInMinutes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment