Sunday 23 September 2018


                                           Date & Time API in JAVA-8 (Joda Time Api)

package dateandtimeapi;

import java.time.LocalDate;
import java.time.LocalTime;

public class CurrentTimeDate {
public static void main(String[] args) {
LocalDate date=LocalDate.now();
System.out.println(date);
LocalTime time=LocalTime.now();
System.out.println(time);
}

}


o/p:---------------------------------------------------------------
2018-09-23
22:49:42.580

===================================================================================================================================
package dateandtimeapi;

import java.time.LocalDateTime;

public class LocalDateTimeClassExample {
public static void main(String[] args) {
LocalDateTime dt=LocalDateTime.now();
int dd=dt.getDayOfMonth();
int mm=dt.getMonthValue();
int yyyy=dt.getYear();
System.out.printf("Date:%d-%d-%d",dd,mm,yyyy);
int h=dt.getHour();
int m=dt.getMinute();
int s=dt.getSecond();
int n=dt.getNano();
System.out.printf("\nTime:%d:%d:%d:%d",h,m,s,n);
}
}


o/p:---------------
Date:23-9-2018
Time:22:50:28:52000000


==================================================================================================================================

package dateandtimeapi;

import java.time.LocalDate;

public class DayMonthYearExample {
public static void main(String[] args) {
LocalDate date=LocalDate.now();
int dd=date.getDayOfMonth();
int mm=date.getMonthValue();
int yyyy=date.getYear();
System.out.println(dd+"----"+mm+"----"+yyyy);
System.out.printf("%d-%d-%d",dd,mm,yyyy);
}

}


o/p:-----------
23----9----2018
23-9-2018



==========================================================================================================================================


package dateandtimeapi;

import java.time.LocalTime;

public class HourMinuteSecondNanoSecondExample {
public static void main(String[] args) {
LocalTime time=LocalTime.now();
System.out.println(time);
int h=time.getHour();
int m=time.getMinute();
int s=time.getSecond();
int n=time.getNano();
System.out.printf("%d:%d:%d:%d",h,m,s,n);
}

}

o/p:----------------------------------
22:56:42.767
22:56:42:767000000

===========================================================================================================================================

package dateandtimeapi;

import java.time.Year;
import java.util.Scanner;

public class LeapYearCheck {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("Enter Year");
int n=s1.nextInt();
Year y=Year.of(n);
if(y.isLeap())
{
System.out.printf("%d year is leap year",n);
}
else
{
System.out.printf("%d year is not a leap year",n);
}
}

}



o/p:--------------------------------------------------------------------------------------------------------
Enter Year
2004
2004 year is leap year

============================================================================================================================================


package dateandtimeapi;

import java.time.LocalDate;
import java.time.Period;


public class PeriodClassExample {
public static void main(String[] args) {
LocalDate birthday=LocalDate.of(1989, 8, 28);
LocalDate today=LocalDate.now();
Period p=Period.between(birthday,today);
System.out.printf("Your age is %d years %d months and %d days",p.getYears(),p.getMonths(),p.getDays());
LocalDate deathday=LocalDate.of(1989+60, 8, 28);
Period p1=Period.between(today,deathday);
int d=p1.getYears()*365+p1.getMonths()*30+p1.getDays();
System.out.printf("\n You will be on earth only %d days...Hurry up to do more important things",d);

}
}

o/p:--------------------------------------------------------------------------------------------

 Your age is 29 years 0 months and 26 days
 You will be on earth only 11285 days...Hurry up to do more important things




==========================================================================================================================================

package dateandtimeapi;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedIdClassExample {
public static void main(String[] args) {
ZoneId zone=ZoneId.systemDefault();
System.out.println(zone);
ZoneId la=ZoneId.of("America/Los_Angeles");
ZonedDateTime dt=ZonedDateTime.now(la);
System.out.println(dt);
}

}

o/p:----------------------------------------------------------------------
Asia/Calcutta
2018-09-23T10:36:17.053-07:00[America/Los_Angeles]


===================================================================================================================================

No comments:

Post a Comment