0% found this document useful (0 votes)
3 views

22. Java Date

Java does not have a built-in Date class, but the java.time package provides functionality for working with date and time. The document includes examples of displaying the current date, time, and both date and time, as well as formatting them using DateTimeFormatter. It also mentions that the ofPattern() method allows for various formatting options.

Uploaded by

Zohaib Javed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

22. Java Date

Java does not have a built-in Date class, but the java.time package provides functionality for working with date and time. The document includes examples of displaying the current date, time, and both date and time, as well as formatting them using DateTimeFormatter. It also mentions that the ofPattern() method allows for various formatting options.

Uploaded by

Zohaib Javed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Date

Date
Java does not have a built-in Date class, but we can
import the java.time package to work with the
date and time API.
Display current Date
import java.time.LocalDate;
public class Main
{
public static void main(String[] args)
{
LocalDate myObj = LocalDate.now();
System.out.println(myObj);
}
}
Display current time
import java.time.LocalTime;
public class Main
{
public static void main(String[] args)
{
LocalTime myObj = LocalTime.now();
System.out.println(myObj);
}
}
Display Current data and time
import java.time.LocalDateTime;
class public class Main
{
public static void main(String[] args)
{
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
}
}
Formatting date and time
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main
{
public static void main(String[] args)
{
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
}
}
Patterns
The ofPattern() method accepts all sorts of values, if you want to
display the date and time in a different format.

You might also like