Last active
April 30, 2017 15:04
-
-
Save solaris33/30eee1a78f16072633338da6eced6763 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ----------------------------------- | |
// 1. Get today's date | |
LocalDate today = LocalDate.now(); | |
System.out.println("Today's Local date : " + today); | |
// Output : Today's Local date : 2014-01-14 | |
// ----------------------------------- | |
// 2. Get today's year, month, and day | |
LocalDate today = LocalDate.now(); | |
int year = today.getYear(); | |
int month = today.getMonthValue(); | |
int day = today.getDayOfMonth(); | |
System.out.printf("Year : %d Month : %d day : %d \t %n", year, month, day); | |
// Output : Today's Local date : 2014-01-14 Year : 2014 Month : 1 day : 14 | |
// ----------------------------------- | |
// 3. Get specific date | |
LocalDate dateOfBirth = LocalDate.of(2010, 01, 14); | |
System.out.println("Your Date of birth is : " + dateOfBirth); | |
// Output : Your Date of birth is : 2010-01-14 | |
// ----------------------------------- | |
// 4. Get current time | |
LocalTime time = LocalTime.now(); | |
System.out.println("local time now : " + time); | |
// Output : local time now : 16:33:33.369 // in hour, minutes, seconds, nano seconds | |
// ----------------------------------- | |
// 5. Plus hours | |
LocalTime time = LocalTime.now(); | |
LocalTime newTime = time.plusHours(2); // adding two hours | |
System.out.println("Time after 2 hours : " + newTime); | |
// Output : Time after 2 hours : 18:33:33.369 | |
// ----------------------------------- | |
// 6. Plus days | |
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); | |
System.out.println("Today is : " + today); | |
System.out.println("Date after 1 week : " + nextWeek); | |
// Output: Today is : 2014-01-14 | |
// Date after 1 week : 2014-01-21 | |
// ----------------------------------- | |
// 7. Plus and minus years | |
LocalDate previousYear = today.minus(1, ChronoUnit.YEARS); | |
System.out.println("Date before 1 year : " + previousYear); | |
LocalDate nextYear = today.plus(1, YEARS); | |
System.out.println("Date after 1 year : " + nextYear); | |
// Output: Date before 1 year : 2013-01-14 | |
// Date after 1 year : 2015-01-14 | |
// ----------------------------------- | |
// 8. Get ZonedDateTime | |
ZoneId america = ZoneId.of("America/New_York"); | |
LocalDateTime localtDateAndTime = LocalDateTime.now(); | |
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america ); | |
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork); | |
// Output : Current date and time in a particular timezone : 2014-01-14T16:33:33.373-05:00[America/New_York] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment