Saturday, 30 July 2016

Java New Date Time API Example

 LocalDate represents only a date.
 LocalTime represents only a time.
 LocalDateTime class represents both date and time.
 Instant represents timestamp.

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);
}


}

No comments:

Post a Comment