Date and Time

Simple date formatter java

public class SimpleDateFormat extends DateFormat

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization.

SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.

public final class LocalDateTime extends Object

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value “2nd October 2007 at 13:45.30.123456789” can be stored in a LocalDateTime.

package com.candidjava.datetime;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

public class FormatDate {
	public static void main(String[] args) {
		//Using SimpleDateFormat Class
		Date date=new Date();
		SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
		System.out.println("dd/MM/yyyy:"+dateFormat.format(date));
		SimpleDateFormat dateFormat1=new SimpleDateFormat("dd-MM-yyyy");
		System.out.println("dd-MM-yyyy:"+dateFormat1.format(date));
		SimpleDateFormat dateFormat2=new SimpleDateFormat("dd-MMM-yyyy");
		System.out.println("dd-MMM-yyyy:"+dateFormat2.format(date));
		SimpleDateFormat dateFormat3=new SimpleDateFormat("EEEE MMMM yyyy");
		System.out.println("EEEE MMMM yyyy:"+dateFormat3.format(date));
		SimpleDateFormat dateFormat4=new SimpleDateFormat("dd-MM-yy");
		System.out.println("dd-MM-yy:"+dateFormat4.format(date));
		SimpleDateFormat dateFormat5=new SimpleDateFormat("MM-dd-yyyy");
		System.out.println("MM-dd-yyyy:"+dateFormat5.format(date));
		SimpleDateFormat dateFormat6=new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("yyyy-MM-dd:"+dateFormat6.format(date));
		SimpleDateFormat dateFormat7=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("yyyy-MM-dd HH:mm:ss:"+dateFormat7.format(date));
		SimpleDateFormat dateFormat8=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		System.out.println("yyyy-MM-dd HH:mm:ss.SSS:"+dateFormat8.format(date));
		SimpleDateFormat dateFormat9=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
		System.out.println("yyyy-MM-dd HH:mm:ss.SSSZ:"+dateFormat9.format(date));
		System.out.println("--------------------------------------------");
		//Using DateTimeFormatter Class
		LocalDateTime newDate=LocalDateTime.now();
		DateTimeFormatter formatter=DateTimeFormatter.ofPattern("dd/MM/yyyy");
		System.out.println("dd/MM/yyyy:"+formatter.format(newDate));
		DateTimeFormatter formatter1=DateTimeFormatter.ofPattern("dd-MM-yyyy");
		System.out.println("dd-MM-yyyy:"+formatter1.format(newDate));
		DateTimeFormatter formatter2=DateTimeFormatter.ofPattern("dd-MMM-yyyy");
		System.out.println("dd-MMM-yyyy:"+formatter2.format(newDate));
		DateTimeFormatter formatter3=DateTimeFormatter.ofPattern("EEEE MMMM yyyy");
		System.out.println("EEEE MMMM yyyy:"+formatter3.format(newDate));
		DateTimeFormatter formatter4=DateTimeFormatter.ofPattern("dd-MM-yy");
		System.out.println("dd-MM-yy:"+formatter4.format(newDate));
		DateTimeFormatter formatter5=DateTimeFormatter.ofPattern("MM-dd-yyyy");
		System.out.println("MM-dd-yyyy:"+formatter5.format(newDate));
		DateTimeFormatter formatter6=DateTimeFormatter.ofPattern("yyyy-MM-dd");
		System.out.println("yyyy-MM-dd:"+formatter6.format(newDate));
		DateTimeFormatter formatter7=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("yyyy-MM-dd HH:mm:ss:"+formatter7.format(newDate));
		DateTimeFormatter formatter8=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
		System.out.println("yyyy-MM-dd HH:mm:ss.SSS:"+formatter8.format(newDate));
		DateTimeFormatter formatter9=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ");
		System.out.println("yyyy-MM-dd HH:mm:ss.SSSZ:"+ZonedDateTime.now().format(formatter9));
	}

}

OUTPUT

dd/MM/yyyy:18/11/2020
dd-MM-yyyy:18-11-2020
dd-MMM-yyyy:18-Nov-2020
EEEE MMMM yyyy:Wednesday November 2020
dd-MM-yy:18-11-20
MM-dd-yyyy:11-18-2020
yyyy-MM-dd:2020-11-18
yyyy-MM-dd HH:mm:ss:2020-11-18 19:48:04
yyyy-MM-dd HH:mm:ss.SSS:2020-11-18 19:48:04.255
yyyy-MM-dd HH:mm:ss.SSSZ:2020-11-18 19:48:04.255+0530
--------------------------------------------
dd/MM/yyyy:18/11/2020
dd-MM-yyyy:18-11-2020
dd-MMM-yyyy:18-Nov-2020
EEEE MMMM yyyy:Wednesday November 2020
dd-MM-yy:18-11-20
MM-dd-yyyy:11-18-2020
yyyy-MM-dd:2020-11-18
yyyy-MM-dd HH:mm:ss:2020-11-18 19:48:04
yyyy-MM-dd HH:mm:ss.SSS:2020-11-18 19:48:04.408
yyyy-MM-dd HH:mm:ss.SSSZ:2020-11-18 19:48:04.629+0530