servlets struts hibernate jsf spring maven
 

Java 8 - New Date/Time API


Local Date-Time API

LocalDate/LocalTime and LocalDateTime classes streamline the improvement where timezones are not required. How about we see them in action.

Create the accompanying java program utilizing any manager of your decision in, state, C:\> JAVA.

 

 

import java.time.LocalDate; 

import java.time.LocalTime; 

import java.time.LocalDateTime; 

import java.time.Month; 

open class Java8Tester { 

open static void main(String args[]) { 

Java8tester = new Java8Tester(); 

java8tester.testLocalDateTime(); 

} 

open void testLocalDateTime() { 

/Get the present date and time 

LocalDateTime currentTime = LocalDateTime.now(); 

System.out.println("Current DateTime: " + currentTime); 

LocalDate date1 = currentTime.toLocalDate(); 

System.out.println("date1: " + date1); 

Month = currentTime.getMonth(); 

int day = currentTime.getDayOfMonth(); 

int seconds = currentTime.getSecond(); 

System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds); 

LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2018); 

System.out.println("date2: " + date2); 

/12 december 2018 

LocalDate date3 = LocalDate.of(2018, Month.DECEMBER, 12); 

System.out.println("date3: " + date3); 

/22 hour 15 minutes 

LocalTime date4 = LocalTime.of(22, 15); 

System.out.println("date4: " + date4); 

/parse a string 

LocalTime date5 = LocalTime.parse("20:15:30"); 

System.out.println("date5: " + date5); 

} 

} 

 

Run model » It should deliver the accompanying yield −
$javac Java8Tester.java
$java - Xmx128M - Xms16M Java8Tester
Current DateTime: 2018-12-29T11:17:54.819 date1: 2018-12-29 Month: DECEMBERday: 29seconds: 54 date2: 2018-12-10T11:17:54.819 date3: 2018-12-12 date4: 22:15 date5: 20:15:30

Zoned Date-Time API

Zoned date-time API is to be utilized when time zone is to be considered. Give us a chance to see them in action.

Create the accompanying Java program utilizing any proofreader of your decision in, state, C:\> JAVA.

 

 

import java.time.ZonedDateTime; 

import java.time.ZoneId; 

open class Java8Tester { 

open static void main(String args[]) { 

Java8tester = new Java8Tester(); 

java8tester.testZonedDateTime(); 

} 

open void testZonedDateTime() { 

/Get the present date and time 

ZonedDateTime date1 = ZonedDateTime.parse("2018-12-03T10:15:30+05:30[Asia/Karachi]"); 

System.out.println("date1: " + date1); 

ZoneId id = ZoneId.of("Europe/Paris"); 

System.out.println("ZoneId: " + id); 

ZoneId currentZone = ZoneId.systemDefault(); 

System.out.println("CurrentZone: " + currentZone); 

} 

} 

 

Run precedent » It should create the accompanying yield −
$javac Java8Tester.java
$java - Xmx128M - Xms16M Java8Tester
date1: 2018-12-03T10:15:30+05:00[Asia/Karachi] ZoneId: Europe/Paris CurrentZone: Etc/UTC

Temporal Adjusters

TemporalAdjuster is utilized to play out the date science. For instance, get the "Second Saturday of the Month" or "Next Tuesday". Give us a chance to see them in action.

Create the accompanying Java program utilizing any editorial manager of your decision in, state, C:\> JAVA.

 

import java.time.LocalDate; 

import java.time.temporal.TemporalAdjusters; 

import java.time.DayOfWeek; 

open class Java8Tester { 

open static void main(String args[]) { 

Java8tester = new Java8Tester(); 

java8tester.testAdjusters(); 

} 

open void testAdjusters() { 

/Get the present date 

LocalDate date1 = LocalDate.now(); 

System.out.println("Current date: " + date1); 

/get the following tuesday 

LocalDate nextTuesday = date1.with(TemporalAdjusters.next(DayOfWeek.TUESDAY)); 

System.out.println("Next Tuesday on : " + nextTuesday); 

/get the second saturday of one month from now 

LocalDate firstInYear = LocalDate.of(date1.getYear(),date1.getMonth(), 1); 

LocalDate secondSaturday = firstInYear.with(TemporalAdjusters.nextOrSame( 

DayOfWeek.SATURDAY)).with(TemporalAdjusters.next(DayOfWeek.SATURDAY)); 

System.out.println("Second Saturday on : " + secondSaturday); 

} 

} 

$javac Java8Tester.java
$java - Xmx128M - Xms16M Java8Tester
Current date: 2018-12-29 Next Tuesday on : 2019-01-01 Second Saturday on : 2018-12-08
Run model »





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition

JAVACODEGEEKS
EmberJS Tutorial