Created
July 12, 2018 18:47
-
-
Save dhawangayash/b81d2680055311262365d33503b17ddd to your computer and use it in GitHub Desktop.
Working with Java LocaleDateTime and Date Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.Instant; | |
import java.time.ZoneId; | |
import java.time.ZoneOffset; | |
import java.util.Date; | |
/** | |
* Must read articles to understand this better | |
* https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate | |
* https://stackoverflow.com/questions/33066904/localdate-to-java-util-date-and-vice-versa-simplest-conversion | |
* https://stackoverflow.com/questions/35183146/how-can-i-create-a-java-8-localdate-from-a-long-epoch-time-in-milliseconds | |
*/ | |
// Conversions to keep the Locale stuff intact: (remember you can go left to right OR from right to left | |
// Date <-> Instance <-> ZonedDateTime <-> LocaleDateTime | |
public class WorkingWithDateTime { | |
public static void main(String[] args) { | |
Date now = Date.from( | |
Instant.ofEpochMilli(System.currentTimeMillis()) | |
.atZone(ZoneId.of("UTC")) | |
.toLocalDateTime() | |
.atZone(ZoneOffset.UTC) | |
.toInstant() | |
); | |
Date threeMonths = Date.from( | |
Instant.now().atZone(ZoneId.of("UTC")).plusMonths(3).toInstant() | |
); | |
if(threeMonths.after(now)) { | |
System.out.println("PASS"); | |
} else { | |
System.out.println("FAIL"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment