Date Class in java
Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.
There are methods and constructors available to customize the data and time in java. Constructors of the date class Let us now see these constructors in action, by implementing them in an example program.
// Java program for demonstrating the constructors of the Date Class
import java.util.*;
public class Main {
public static void main(String[] args) {
Date dt1 = new Date();
System.out.println("Today's date is " + dt1);
Date dt2 = new Date(2323223233L);
System.out.println("GIven data is represented as " + dt2);
}
}
Today's date is Fri Oct 09 13:27:13 UTC 2020
GIven data is represented as Tue Jan 27 21:20:23 UTC 1970
// Example program for demonstrating methods of Date class
import java.util.*;
public class Main {
public static void main(String[] args) {
// Creating dates
Date dt1 = new Date(2003, 12, 22);
Date dt2 = new Date();ย ย ย ย ย ย // Today's date
Date dt3 = new Date(2010, 12, 7);
boolean afr = dt1.after(dt2);
System.out.println("Date 2003/12/22 comes after " + "today's date: " + afr);
boolean bfr = dt2.before(dt3);
System.out.println("Today's date comes before " + "date 2010/12/7: " + bfr);
int com = dt3.compareTo(dt2);
System.out.println(com);
System.out.println(" Date Before setting: " + dt2);
dt2.setTime(204587433443L);
System.out.println("Date After setting with setTime: " + dt2);
}
}
Date 2003/12/22 comes after today's date: true
Today's date comes before date 2010/12/7: true
1
Date Before setting: Fri Oct 09 13:38:52 UTC 2020
Date After setting with setTime: Fri Jun 25 21:50:33 UTC 1976
Now customizing these date formats will require us to use the DateFormat class, the SimpleDateFormat class, or the offpattern() method of the DateTimeFormatter class. So let us see each of them one by one in the following sequence
Let us format dates with each of these classes, starting with the DateFormat class.Formatting dates with the DateFormat class in java
The DateFormat class is an asynchronous class,ย present at java.text.DateFormat.This class provides various methods to format dates and also to parse the dates to a String.
Let us see an example for implementing the DateFormat class. Converting Date to a String in java Let use the DateFormat class, for converting a given date to a string.
// Example Program to format date with the DateFormat class
import java.util.*;
import java.text.*;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
DateFormat Date = DateFormat.getDateInstance();// date formatter
Calendar cal = Calendar.getInstance(); // calender object
System.out.println("The actual Date: " + cal.getTime());
// Using format() method of DateFormat for converting date to string
String ForD = Date.format(cal.getTime());
System.out.println("Formatted Date: " + ForD);
}
}
The actual Date: Fri Oct 09 13:55:28 UTC 2020
Formatted Date: Oct 9, 2020
Check out this Complete Java Online Training by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework, and Spring with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell, by expert software developers with over 10 years of experience in the field to make you an industry required certified java developer.
Formatting dates with the SimpleDateFormat class in java
This is the child class of DateFormat class which takes a String argument that should specify the pattern or formatting pattern of the date. The years should be represented by โyyyyโ,ย month by โMMโ and the date by โddโ. Let us implement the SimpleDateFormat class with an example program.Converting String to a Date in java
// Example Program to format date with the DateFormat class
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String pattern = "dd/MM/yyyy";
SimpleDateFormat DateFor = new SimpleDateFormat(pattern);
try {
Date date = DateFor.parse("20/05/2016");
System.out.println("Date : " + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Date : Fri May 20 00:00:00 UTC 2016
Formatting dates with DateTimeFormatter class in java
The ofpattern() method of the DateTimeFormatter class can be used on LocalDateTime instance, to format or parse the date or time object of the class.
// Example Program to format date with the DateTimeFormatter class
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime dtobj = LocalDateTime.now();
System.out.println("Date before formatting: " + dtobj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy, HH-mm-ss");
String fdt = dtobj.format(myFormatObj);
System.out.println("Date after formatting: " + fdt);
}
}
Date before formatting: 2020-10-09T14:21:51.602193
Date after formatting: 09-10-2020, 14-21-51
This was all about strings, arrays, and string arrays in java along with practice programs. To get in-depth knowledge of the core Java and advanced java, J2EEย SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect-Oriented Programming (AOP) architecture, Hibernate Framework, and Struts through JDBC you can enroll in Certified Java Training in Chennai or Certified Java Training in Bangalore by FITA or a virtual class for this course, at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.
FITAโs courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.