This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Here we learn New date time api of java-8.
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Year;
public class NewDateAndTimeAPI {
public static void main(String[] args) {
System.out.println("WOKING WITH DATES\n--------------------------");
// to increase date by plus 1
LocalDate tommarowDate = LocalDate.now().plusDays(1);
System.out.println("NEXT DAY DATE\t:"+tommarowDate);
// to increase month by plus 1
LocalDate nextMonthDate = LocalDate.now().plusMonths(1);
System.out.println("NEXT MONTH DATE\t:"+nextMonthDate);
// to increase year by plus 1
LocalDate nextYearDate = LocalDate.now().plusYears(1);
System.out.println("NEXT YEAR DATE\t:"+nextYearDate);
//to increase date day and year all by plus 1
LocalDate next = LocalDate.now().plusDays(1).plusMonths(1).plusYears(1);
System.out.println("DATE DAY AND YEAR ALL INCREASES BY PLUS 1 : "+next);
//to get only current year
Year currentYear = Year.now();
System.out.println("CURRENT YEAR\t:"+currentYear);
//to get only current month
Month currentMonth = LocalDate.now().getMonth();
System.out.println("CURRENT MONTH\t:"+currentMonth);
//to get only current date
int currentDate = LocalDate.now().getDayOfMonth();
System.out.println("TODAY DATE\t:"+currentDate);
//to get only current week day
DayOfWeek week = LocalDate.now().getDayOfWeek();
System.out.println("TODAY DAY OF WEEK\t:"+week);
//to check weather current or any year is leap year or not
System.out.println("CURRENY YEAR IS LEAP YEAR OR NOT : "+LocalDate.now().isLeapYear());
//to check manually any year is leap or not
boolean checkLeap = Year.isLeap(2017);
System.out.println("MANNUALLY CHECKING 2017 IS LEAP OR NOT : "+checkLeap);
System.out.println("---------------------------------\nWORKING WITH TIME\n---------------------------------");
// to know current time only without date
LocalTime currentTime = LocalTime.now();
System.out.println("CURRENT TIME\t:"+currentTime);
// to know current hour from current time
int currentHour = LocalTime.now().getHour();
System.out.println("CURRENT HOUR\t:"+currentHour);
// to know current minute from current time
int currentMinute = LocalTime.now().getMinute();
System.out.println("CURRENT MINUTE\t:"+currentMinute);
// to know current second from current time
int currentSecond = LocalTime.now().getSecond();
System.out.println("CURRENT SECOND\t:"+currentSecond);
// to know current second from current time
int currentMillisecond = LocalTime.now().getNano();
System.out.println("CURRENT NANO SECOND\t:"+currentMillisecond);
System.out.println("---------------------------------\nWORKING WITH DATE & TIME\n---------------------------------");
// to know current date and time
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("CURRENT DATE AND TIME\t:"+dateTime);
System.out.println("---------------------------------\nWORKING WITH TIMESTAMP\n---------------------------------");
// to work with timestamp
Instant ins = Instant.now();
System.out.println(ins);
OffsetTime off = OffsetTime.now();
System.out.println(off);
}
}
Program Output :-
WOKING WITH DATES
--------------------------
NEXT DAY DATE :2016-10-06
NEXT MONTH DATE :2016-11-05
NEXT YEAR DATE :2017-10-05
DATE DAY AND YEAR ALL INCREASES BY PLUS 1 : 2017-11-06
CURRENT YEAR :2016
CURRENT MONTH :OCTOBER
TODAY DATE :5
TODAY DAY OF WEEK :WEDNESDAY
CURRENY YEAR IS LEAP YEAR OR NOT : true
MANNUALLY CHECKING 2017 IS LEAP OR NOT : false
---------------------------------
WORKING WITH TIME
---------------------------------
CURRENT TIME :15:07:21.976
CURRENT HOUR :15
CURRENT MINUTE :7
CURRENT SECOND :21
CURRENT NANO SECOND :976000000
---------------------------------
WORKING WITH DATE & TIME
---------------------------------
CURRENT DATE AND TIME :2016-10-05T15:07:21.976
---------------------------------
WORKING WITH TIMESTAMP
---------------------------------
2016-10-05T22:07:21.976Z
15:07:21.976-07:00

No comments:
Post a Comment