Java 8 new Date and Time API -2

In this previous post we already check about Date api now in this post will take a look on Time api example.

Time API Example: 

With the help of LocalTime we will get the hour, minute, second, nanosecond

package com.blogspot.ekumeedhelp;

import java.time.*;

public class JavaDateAPIExample2 {

	public static void main(String[] args) {

		LocalTime time = LocalTime.now();
		int hour = time.getHour();
		int minute = time.getMinute();
		int second = time.getSecond();
		int nanoSecond = time.getNano();
		System.out.printf("\n%d:%d:%d:%d", hour, minute, second, nanoSecond);
	}
}

Now let's assume if you want both Date and Time for this we have LocalDateTime class.

DateTime API example :

package com.blogspot.ekumeedhelp;

import java.time.*;

public class JavaDateAPIExample2 {

	public static void main(String[] args) {

	LocalDateTime currentDateTime = LocalDateTime.now();

 	System.out.printf(currentDateTime);
 
        LocalDateTime customeDate = LocalDateTime.of(2020,Month.APRIL,22,12,45); 
 
        System.out.printf(customeDate); 

	}
}


For more details please check out this video

No comments:

Post a Comment