Day 9 : Date and Time in Java

Day 9 : Date and Time in Java

Date and Time :

In Java, managing date and time involves several classes from the java.time package, introduced in Java 8. The package includes many date and time classes.

  • LocalDate
  • LocalTime
  • LocalDateTime
  • DateTimeFormatter

Display Current Date :

  • To display the current date, import the java.time.LocalDate class, and use its now() method.
  • Represents a date (year, month, day (yyyy-MM-dd)).

Example :

import java.time.LocalDate; // import the LocalDate class
public class LocalDateExample {
  public static void main(String[] args) {
    LocalDate date = LocalDate.now(); // Create a date object
    System.out.println(date); // Display the current date
  }
}        
Output :
2024-07-12        

In this example :

  • Imported the LocalDate class from the java.time package. This allows you to use LocalDate in your program without fully qualifying it every time (i.e., java.time.LocalDate).
  • In the main method, LocalDate date = LocalDate.now(); creates a new LocalDate object named date that represents the current date. The now() method of LocalDate class returns the current date based on the system clock in the default time-zone.
  • Finally, printed the value of date to the console. Since LocalDate overrides the toString() method to provide a standard ISO-8601 format (yyyy-MM-dd), it prints the current date in that format.

Display Current Time :

  • To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method.
  • Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns)).

Example :

import java.time.LocalTime; // import the LocalTime class
public class LocalTimeExample {
  public static void main(String[] args) {
    LocalTime time = LocalTime.now();
    System.out.println(time);
  }
}        
Output  :
14:02:22.218770100        

In this Example :

  • Imported the LocalTime class from the java.time package. This allows you to use LocalTime in your program without fully qualifying it every time (i.e., java.time.LocalTime).
  • In the main method, LocalTime time = LocalTime.now(); creates a new LocalTime object named time that represents the current time. The now() method of LocalTime class returns the current time based on the system clock in the default time-zone.
  • Finally, prints the value of time to the console. Since LocalTime overrides the toString() method to provide a standard ISO-8601 format (HH:mm.sss), it prints the current time in that format.

Display Current Date and Time :

  • To display the current date and time, import the java.time.LocalDateTime class, and use its now() method.
  • Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns).

Example :

import java.time.LocalDateTime; // import the LocalDateTime class
public class LocalDateTimeExample {
  public static void main(String[] args) {
    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println(myObj);
  }
}        
Output :
2024-07-12T14:15:25.770661100        

In this example :

  • Imported the LocalDateTime class from the java.time package. This allows you to use LocalDateTime in your program without fully qualifying it every time (i.e., java.time.LocalDateTime).
  • In the main method, LocalDateTime dateTime = LocalDateTime.now(); creates a new LocalDateTime object named dateTime that represents the current date and time. The now() method of LocalDateTime class returns the current date and time based on the system clock in the default time-zone.
  • Finally, printed the value of dateTime to the console. Since LocalDateTime overrides the toString() method to provide a standard ISO-8601 format (yyyy-MM-ddTHH:mm:ss.sss), it prints the current date and time in that format.

Formatting Date and Time :

The "T" in the displayed in LocalDateTime is used to separate the date from the time. You can use the DateTimeFormatter class with the ofPattern() method in the same package to format or parse date-time objects.

import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class

public class FormattingDateTimeExample {
  public static void main(String[] args) {
    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println("Before formatting: " + dateTime);
    DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
    String formattedDate = dateTime.format(formatDateTime);
    System.out.println("After formatting: " + formattedDate);
  }
}        
Output :
Before formatting: 2024-07-12T15:02:24.344718300
After formatting: 12-07-2024 15:02:24        

In this example :

  • Imported java.time.LocalDateTime; imports the LocalDateTime class from the java.time package.
  • Imported java.time.format.DateTimeFormatter; imports the DateTimeFormatter class from the java.time.format package. This class is used to format and parse dates and times.
  • In the main method, LocalDateTime formatDateTime = LocalDateTime.now(); creates a new LocalDateTime object named formatDateTime that represents the current date and time. The now() method of LocalDateTime class returns the current date and time based on the system clock in the default time-zone.
  • System.out.println("Before formatting: " + dateTime); prints the original LocalDateTime object dateTime before formatting. By default, LocalDateTime prints in ISO-8601 format (yyyy-MM-ddTHH:mm:ss).
  • DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); creates a DateTimeFormatter object formatDateTime with a specific pattern "dd-MM-yyyy HH:mm:ss". This pattern specifies how the date and time should be formatted.
  • String formattedDate = dateTime.format(formatDateTime); formats the LocalDateTime object dateTime into a string formattedDate using the specified DateTimeFormatter.
  • System.out.println("After formatting: " + formattedDate); prints the formatted date and time string formattedDate.

要查看或添加评论,请登录

POOJAASHREE P V的更多文章

  • Day 13 : File Handling in Java

    Day 13 : File Handling in Java

    File Class : The File class of the java.io package is used to perform various operations on files and directories.

  • Day 12 : Scanner Class in Java

    Day 12 : Scanner Class in Java

    Scanner Class : Scanner class in Java is found in the java.util package.

  • Day 11 : Exception Handling in Java

    Day 11 : Exception Handling in Java

    Exception : An exception is an event that disrupts the normal flow of the program. It is an object which is thrown at…

  • Day 10 : Arrays in Java

    Day 10 : Arrays in Java

    Arrays : Array is an object which contains elements of a similar data type. The elements of an array are stored in a…

  • Day 8 : String, String Methods, String Builder and String Buffer in Java

    Day 8 : String, String Methods, String Builder and String Buffer in Java

    String : String is an object that represents a sequence of characters. The java.

  • Day 7 : Math Class and Math Methods in Java

    Day 7 : Math Class and Math Methods in Java

    Math Class : Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos()…

  • Day 6 : Methods in Java

    Day 6 : Methods in Java

    Method : A method is a block of code or collection of statements or a set of code grouped together to perform a certain…

  • Day 5 : Looping Statements in Java

    Day 5 : Looping Statements in Java

    Looping Statements : Looping statements are used to execute a block of code repeatedly based on certain conditions…

    1 条评论
  • Day 4 : Conditional Statements in Java

    Day 4 : Conditional Statements in Java

    Conditional Statements : Conditional statements are control flow statements that allow you to execute different blocks…

  • Day 3 : Variables, Data Types and Operators

    Day 3 : Variables, Data Types and Operators

    Variables : A variable is a container which holds the value while the Java program is executed. A variable is assigned…

社区洞察

其他会员也浏览了