Tutorialspoint

Java Program to create custom DateTime formatter

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
public class Demo {
	public static void main(String[] args) {
		DateTimeFormatter dtFormat =  new DateTimeFormatterBuilder()
		.appendValue(ChronoField.HOUR_OF_DAY)
		.appendLiteral(":")
		.appendValue(ChronoField.MINUTE_OF_HOUR)
		.appendLiteral(":")
		.appendValue(ChronoField.SECOND_OF_MINUTE)
		.toFormatter();
		System.out.println("Time = "+dtFormat.format(LocalDateTime.now()));
		dtFormat =  new DateTimeFormatterBuilder()
		.appendValue(ChronoField.YEAR)
		.appendLiteral("/")
		.appendValue(ChronoField.MONTH_OF_YEAR)
		.appendLiteral("/")
		.appendValue(ChronoField.DAY_OF_MONTH)
		.toFormatter();
		System.out.println("Date = "+dtFormat.format(LocalDateTime.now()));
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.