|
|
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.
DateParsingFormatEx |
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);
}
}
|
|
Output: |
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
|
|
|
|
|
Java-8 Date and Time API Examples
- Java 8 LocalDateTime example.
- Java 8 ZonedDateTime example.
- Java 8 LocalDate example.
- Java 8 LocalTime example.
- Create date time with custom values in Java 8.
- Java 8 Instant (Timestamp) example.
- Java 8 Period example.
- Java 8 Duration example.
- Java 8 Date parsing and formatting example.
- How to compare two dates in Java 8?
- How to get current date and time Java 8?
- How to calculate execution / elapsed time Java 8?
- How to convert string to Date Java 8?
- How to add/substract days to Date in Java 8?
|
|
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object
according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that
determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float,
double, boolean, char.
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill
|