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 :
- Using LocalDateTime
- Using ZonedDateTime
- 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:
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Happy Coding !!
Happy Learning !!