GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||
GeezerVison is showing codescracker.com โ Java Date and Time
Java Date and TimeJava's efficient date and time handling is one of its most appealing features. Java's extensive library of date- and time-related classes and methods simplifies the work of managing and manipulating such values. Every Java developer needs to be able to work with dates and times, whether they're building a simple app or a complex system. This article will introduce you to Java's date and time capabilities, including some basic examples to get you started. Print the current date in JavaIn Java, we can use the "java.util.Date" class to print the current date. As an example: import java.util.Date;
public class JavaDateExample {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate);
}
}
When I execute this Java program, I get the output shown in the snapshot given below. However, you will get different output based on the current date, but the format will be the same. Since the current date was "Tue Apr 11 14:07:59 IST 2023" when I executed this program, I got this output. This Java program imports the "Date" class from the "java.util" package and defines a class named "JavaDateExample." Within the "main" method, a new "Date" object is created and assigned to the "currentDate" variable using the constructor of the "Date" class. You will learn about constructors later in this Java tutorial series, after covering the basics of object-oriented programming, which are classes and objects. Now coming to the point, this object represents the current date and time based on the system clock of the computer running the program. Then, the program prints the string "Current Date: " followed by the value of the "currentDate" object using the "System.out.println" method, which outputs the string to the output console. Another way to print the current date (excluding time) and in the format "YYYY-MM-DD", you can use the "java.time.LocalDate" as shown in the example given below: import java.time.LocalDate;
public class DateExample {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
}
}
Now the output should be: However, you can format the date using the "SimpleDateFormat" class based on your requirements. For example: import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormattingExample {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedDate = formatter.format(currentDate);
System.out.println("Date: " + formattedDate);
}
}
In this example, the SimpleDateFormat object is created with a format pattern of "dd-MM-yyyy HH:mm:ss". This format pattern represents the day (dd), month (MM), year (yyyy), hour (HH), minute (mm), and second (ss) of the Date object. So I get the following output this time: Print the current time in JavaYou can format and print the current time in Java with the help of the program above. As an example: import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeExample {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String formattedDate = formatter.format(currentDate);
System.out.println("Current Time: " + formattedDate);
}
}
The output should be: Or you can create a separate program using the "LocalTime" class of the "java.time" package. Consider the following Java program as a demonstration of printing the current time in Java. import java.time.LocalTime;
public class CurrentTimeExample {
public static void main(String[] args) {
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
}
}
This program produces the following output: I said "this program produces" does not mean that we will get the above output all the time; instead, it depends on the current time when executing this program ๐. I also created a separate post that contains the programs for printing dates and times in Java. The following table lists and briefly describes the most commonly used classes when working with date and time in Java.
ยซ Previous Tutorial Next Tutorial ยป Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. | ||||||||||||||||
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |