JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 LocalDateTime example.


LocalDateTime is a date-time without a time-zone in the ISO-8601 calendar system, such as 2016-12-03T10:15:30.

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision.

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

LocalDateTime class is immutable and thread-safe.

DateTimeIntroEx
package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;

public class DateTimeIntroEx {

	public static void main(String[] args) {

		// get current date and time
		LocalDateTime currTime = LocalDateTime.now();
		System.out.println("current date time: "+currTime);

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

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

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

Output:
current date time: 2016-12-15T11:50:36.955
current date: 2016-12-15
Month: 12
Day of the month: 15
Year: 2016
current hour: 11
current minutes: 50
current seconds: 36
 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
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
You can never get enough of what you don’t really need.
-- Eric Hoffer

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 © 2022 by Nataraja Gootooru. All Rights Reserved.