Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java 8 Date Time Api

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 879
    Comment on it

    Date Time Api in Java 8: Java 8 introduce with new Date Time API. which is much simpler than the old date time api and reduce the overheads faces in the old api  some of the changes are describe below in new date time api.

    1. LocalDate/LocalTime: LocalDate and LocalTime Classes is introduce where timezones are not required.

     // current date and time
     LocalDateTime currentTime = LocalDateTime.now();
     System.out.println("Current DateTime: " + currentTime);
                    
     LocalDate date1 = currentTime.toLocalDate();
     System.out.println("date1: " + date1);
                    
     Month month = currentTime.getMonth();
     int day = currentTime.getDayOfMonth();
     int seconds = currentTime.getSecond();
                    
     System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);


                    
       2. Zone DateTime API: It is used when time zone is to be considered.
                

     // current date and time
    
     ZonedDateTime date = ZonedDateTime.parse("2016-01-05T10:14:20+06:30[Asia/Kolkata]");
     System.out.println("date: " + date);


       3.ChronoUnit Enum: ChronoUnit enum is added in the new Java 8 API which is Used to represent day, month, etc and it is available in java.time.temporal package.

    //Get the current date
    LocalDate currentDate = LocalDate.now();
    System.out.println("Current date: " + currentDate);
                
    //add 1 week to the current date
    LocalDate nextWeek = currentDate.plus(1, ChronoUnit.WEEKS);
    System.out.println("Next week: " + nextWeek);
              
    //add 2 month to the current date
    LocalDate nextMonth = currentDate.plus(2, ChronoUnit.MONTHS);
    System.out.println("Next month: " + nextMonth);
                
    //add 3 year to the current date
    LocalDate nextYear = currentDate.plus(3, ChronoUnit.YEARS);
    System.out.println("Next year: " + nextYear);
                
    //add 10 years to the current date
    LocalDate nextDecade = currentDate.plus(1, ChronoUnit.DECADES);
    System.out.println("Next ten year: " + nextDecade);

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: