Java 8 – How to get Date/time with AM/PM marker and Zone ?

In this article, we will learn how to get Date/time along with AM/PM marker and Zone information

Get Date/time with AM/PM marker & Zone info :

  1. Using LocalDateTime
  2. Using ZonedDateTime
  3. Using Date

1. Using LocalDateTime :

  • Get LocalDateTime using LocalDateTime.now() method
  • Format LocalDateTime by passing DateTimeFormatter with pattern (dd-MMM-yyyy hh:mm:ss a)
  • Note:
    • HH – denotes 24-hour clock without AM/PM marker
    • hh – denotes 12-hour clock with optional AM/PM marker
  • For getting Zone information use ZoneId.systemDefault() method

GetAmPmMarkerAndZoneInfoUsingLocalDateTime.java

package in.bench.resources.java8.date.examples;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class GetAmPmMarkerAndZoneInfoUsingLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current system date/time is :- \n" 
				+ localDateTime);


		// 2. get default system zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault Zone is :- \n" 
				+ zoneId);


		// 3. get LocalDateTime with Zone & AM/PM marker
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd-MMM-yyyy hh:mm:ss a");
		String formatterDateTime = dateTimeFormatter.format(localDateTime);
		System.out.print("\nFormatted Date/time with Zone is :- \n" 
				+ formatterDateTime + " " + zoneId);
	}
}

Output:

Current system date/time is :- 
2022-08-29T17:33:30.697003600

Default Zone is :- 
Asia/Calcutta

Formatted Date/time with Zone is :- 
29-Aug-2022 05:33:30 pm Asia/Calcutta

2. Using ZonedDateTime :

  • Get ZonedDateTime using ZonedDateTime.now() method
  • Format ZonedDateTime by passing DateTimeFormatter with pattern (dd-MMM-yyyy hh:mm:ss a z)
  • Note:
    • HH – denotes 24-hour clock without AM/PM marker
    • hh – denotes 12-hour clock with optional AM/PM marker
    • z – denotes zone information like IST, JPT, CET, etc.

GetAmPmMarkerAndZoneInfoUsingZonedDateTime.java

package in.bench.resources.java8.date.examples;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class GetAmPmMarkerAndZoneInfoUsingZonedDateTime {

	public static void main(String[] args) {

		// 1. get current system date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Current Zoned date/time is :- \n" + zonedDateTime);


		// 2. get ZonedDateTime with Zone & AM/PM marker
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("dd-MMM-yyyy hh:mm:ss a z");
		String formatterDateTime = dateTimeFormatter.format(zonedDateTime);
		System.out.print("\nFormatted Date/time with Zone is :- \n" + formatterDateTime);
	}
}

Output:

Current Zoned date/time is :- 
2022-08-29T17:33:54.109318800+05:30[Asia/Calcutta]

Formatted Date/time with Zone is :- 
29-Aug-2022 05:33:54 pm IST

3. Using Date :

  • Get current Date/time by instantiating Date object
  • Format Date by using SimpleDateFormat with pattern (dd-MMM-yyyy hh:mm:ss a z)
  • Note:
    • HH – denotes 24-hour clock without AM/PM marker
    • hh – denotes 12-hour clock with optional AM/PM marker
    • z – denotes zone information like IST, JPT, CET, etc.

GetAmPmMarkerAndZoneInfoUsingDate.java

package in.bench.resources.java8.date.examples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetAmPmMarkerAndZoneInfoUsingDate {

	public static void main(String[] args) {

		// 1. get date/time
		Date currentDateTime = new Date();
		System.out.println("Current date/time is :- \n" + currentDateTime);


		// 2. get Date with Zone & AM/PM marker
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a z");
		String formatterDateTime = dateFormat.format(currentDateTime);
		System.out.print("\nFormatted Date/time with Zone is :- \n" + formatterDateTime);
	}
}

Output:

Current date/time is :- 
Mon Aug 29 17:34:22 IST 2022

Formatted Date/time with Zone is :- 
29-08-2022 05:34:22 pm IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Timestamp to Calendar and vice-versa ?
Java 8 – How to convert Date to XMLGregorianCalendar and vice-versa ?