Publish Your Article Here

Java 8 Date parsing and formatting example.

Java 8 date and time API makes date formatting and parsing very simple. This page gives many examples on parsing and formatting date.

package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateParsingFormatEx {

        public static void main(String[] args) {

                // create a date time object (2016-01-01 10:30)
                LocalDateTime dtObj = LocalDateTime.of(2016, Month.JANUARY, 1, 10, 35);
                System.out.println("date time obj: "+dtObj);

                // format ISO date time (2016-01-01T10:35:00)
                String isoObj = dtObj.format(DateTimeFormatter.ISO_DATE_TIME);
                System.out.println("date time obj in ISO format: "+isoObj);

                // format basic ISO date format (20160101)
                String basicIsoObj = dtObj.format(DateTimeFormatter.BASIC_ISO_DATE);
                System.out.println("date time obj in basic ISO format: "+basicIsoObj);

                // format as ISO week date (2015-W53-5)
                String isoWeekObj = dtObj.format(DateTimeFormatter.ISO_WEEK_DATE);
                System.out.println("date time obj in ISO week format: "+isoWeekObj);

                // french date formatting (1. janvier 2016)
                String frenchDate = dtObj.format(DateTimeFormatter.ofPattern("d. MMMM yyyy", new Locale("fr")));
                System.out.println("date time obj in french date format: "+frenchDate);

                // user custom pattern (01/01/16)
                String custDtObj = dtObj.format(DateTimeFormatter.ofPattern("dd/MM/yy"));
                System.out.println("date time obj in custom format: "+custDtObj);

                // parsing string to date object
                LocalDate isoDateObj = LocalDate.parse("2016-02-22");
                System.out.println("iso date: "+isoDateObj);

                // custom date string to date object
                LocalDate custPatternObj = LocalDate.parse("02-2016-22", DateTimeFormatter.ofPattern("MM-yyyy-dd"));
                System.out.println("custom date format: "+custPatternObj);

date time obj: 2016-01-01T10:35
date time obj in ISO format: 2016-01-01T10:35:00
date time obj in basic ISO format: 20160101
date time obj in ISO week format: 2015-W53-5
date time obj in french date format: 1. janvier 2016
date time obj in custom format: 01/01/16
iso date: 2016-02-22
custom date format: 2016-02-22
<< 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
Java class can be private?
We can not declare top level class as private. Java allows only public and default modifier for top level classes in java. Inner classes can be private.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

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