Java — How to format Date and Time | Code Factory

Format LocalDate using DateTimeFormatter

package com.example.java.programming.datetime;import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class LocalDateFormatExample {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.of(2020, 6, 29);
System.out.println(localDate.format(dateTimeFormatter));
}
}
29/06/2020

Format LocalDateTime using DateTimeFormatter

package com.example.java.programming.datetime;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class LocalDateTimeFormatExample {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a");
LocalDateTime localDateTime = LocalDateTime.of(2020, 6, 29, 15, 30, 05);
System.out.println(localDateTime.format(dateTimeFormatter));
}
}
Mon, Jun 29 2020, 03:30:05 PM

Format ZonedDateTime using DateTimeFormatter

package com.example.java.programming.datetime;import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author code.factory
*
*/
public class ZonedDateTimeFormatExample {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a (VV)");
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2020, 6, 29, 15, 35, 45),
ZoneId.of("Asia/Kolkata"));
System.out.println(zonedDateTime.format(dateTimeFormatter));
}
}
Mon, Jun 29 2020, 03:35:45 PM (Asia/Kolkata)

Format Date using SimpleDateFormat

package com.example.java.programming.datetime;import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author code.factory
*
*/
public class DateFormatExample {
public static void main(String... args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
System.out.println(sdf.format(date));
}
}
29/06/2020

Format Date and Time using SimpleDateFormat

package com.example.java.programming.datetime;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author code.factory
*
*/
public class DateTimeFormatExample {
public static void main(String... args) {
SimpleDateFormat sdf = new SimpleDateFormat("E, MMM dd yyyy, hh:mm:ss a");
Calendar calendar = Calendar.getInstance();
calendar.set(2020, 5, 29, 15, 45, 45); // 0-Jan, 1-Feb, ...
Date date = calendar.getTime();
System.out.println(sdf.format(date));
}
}
Mon, Jun 29 2020, 03:45:45 PM

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Swift basics clean bowled - when let / var is optional (?)

ttinolja Co.,

Prepositions of time: vor, seit

Things That You Need To Know About Wordpress

Git — how to commit

Count Cars On The Highway Using C# Computer Vision

Building Full Stack Development Skills on Your Team

Install InvoiceNinja on Kubernetes with Rancher surface and Traefik

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Functional Interfaces in Java

Java Virtual Machine

Prototype Design Pattern

Java libraries (Java API)