yt

Header Ads

Chapter -17 What is Time And Date in Java

Time And Date in Java

Introduction : 

Time and Date function in java provides a lot of feature that are used to manipulate now current date, time time and time with one time in program methods used and import same package in java . By using java.time package we can get current date and time in very simple way. Let's use some important methods of java.time package. 

Note : If you don't known what is package please watch a tutorial on YOUTUBE for better understand so lets go for understand some program in java.

Example 1: Print Current Date 

Note : LocalDate package represent a date(YEAR-MONTH-DAY) 
           For example: 2022-07-06


import java.time.LocalDate;   // import package/ class

class Date{
  public static void main(String args[]){
    LocalDate date = LocalDate.now();   // Create a object name of date
    System.out.println("Current Date is :"+date);     // display current date line.
  }
}


----OUTPUT----


Current Date : 2022-07-06


Example 2:  Print Current Time

Note : LocalTime package represent a Time(hour, minute, second and nanoseconds (HH-mm-ss-ns))) 
           For example: 17:39:32.442501900



import java.time.LocalTime;   // import package/ class

class Time{
  public static void main(String args[]){
    LocalTime time = LocalTime.now();   // Create a object name of date
    System.out.println("Current Time is :"+time);     // display current date line.
  }
 
}



---OUTPUT---


Current Time is :17:39:32.442501900



Example 3:  Print Current Date and Time 

Note : import LocalDateTime package represent both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
           For example: 2022-07-06T17:53:45.883243900



import java.time.LocalTime;   // import package/ class

class Time{
  public static void main(String args[]){
    LocalTime time = LocalTime.now();   // Create a object name of date
    System.out.println("Current Time is :"+time);     // display current date line.
  }
 
}




---OUTPUT---


Current DateTime:2022-07-06T17:53:45.883243900


Example 3:  Print Current Date Month and Year using Calendar. 

Note : import util calendar package represent both a date and a time (yyyy-MM-dd-HH-mm-ss-ns).



import java.util.Calendar; // import package

class Calen{
  public static void main(String args[]){
   
    Calendar cal=Calendar.getInstance();  // create object cal
     System.out.println("Current Date:"+cal.get(Calendar.DATE));    // print date
     System.out.println("Current Month:"+(cal.get(Calendar.MONTH)+1));  // print month
     System.out.println("Current Year:"+cal.get(Calendar.YEAR));  // print year
  }
}





---OUTPUT---


Current Date:6
Current Month:7 Current Year:2022







    No comments

    Theme images by Dizzo. Powered by Blogger.