Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, 10 June 2021

How to print the date in specific format in java?

You can format the dates in java using SimpleDateFormat class from java.text pacakge.

Create an object of SimpleDateFormat - provide the pattern to constructor.
Note: Pattern will have date specific literals.

String pattern = "yy-MM-dd";

SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);

For example: 

package com.algos;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {
public static void main(String[] args) {
String pattern = "dd-MM-yy";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
System.out.println(dateFormat.format(new Date()));
}
}