JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Java 8 ZonedDateTime example.


ZonedDateTime class is a date-time with a time-zone in the ISO-8601 calendar system, such as 2016-12-03T10:15:30+01:00 Europe/Paris.

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.

In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId. The ZoneOffset is a vital, but secondary, piece of information, used to ensure that the class represents an instant, especially during a daylight savings overlap.

ZonedDateTime class is immutable and thread-safe.

ZonedDateTimeEx
package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeEx {

	public static void main(String[] args) {

		ZonedDateTime zoneDateTime = ZonedDateTime.now();
		System.out.println("current date time with zone: "+zoneDateTime);

		// get date alone
		LocalDate d1 = zoneDateTime.toLocalDate();
		System.out.println("current date: " + d1);

		// get zone info
		ZoneId zone = zoneDateTime.getZone();
		System.out.println("zone info: " + zone.getId());

		// extract date properties
		Month mnt = zoneDateTime.getMonth();
		System.out.println("Month: "+mnt.getValue());
		int dayOfMonth = zoneDateTime.getDayOfMonth();
		System.out.println("Day of the month: "+dayOfMonth);
		int year = zoneDateTime.getYear();
		System.out.println("Year: "+year);

		// extract time properties
		int hour = zoneDateTime.getHour();
		System.out.println("current hour: "+hour);
		int minutes = zoneDateTime.getMinute();
		System.out.println("current minutes: "+minutes);
		int seconds = zoneDateTime.getSecond();
		System.out.println("current seconds: "+seconds);
	}
}

Output:
current date time with zone: 2016-12-15T12:15:06.898+05:30[Asia/Kolkata]
current date: 2016-12-15
zone info: Asia/Kolkata
Month: 12
Day of the month: 15
Year: 2016
current hour: 12
current minutes: 15
current seconds: 6
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.