Java – How to format Date and Time | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Format Date and Time represented using Date, LocalDate, LocalDateTime, or ZonedDateTime to a readable String in Java.

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));
	}
}

Output :

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));
	}
}

Output :

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));
	}
}

Output :

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));
	}
}

Output :

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));
	}
}

Output :

Mon, Jun 29 2020, 03:45:45 PM

2 thoughts on “Java – How to format Date and Time | Code Factory”

Leave a comment