Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly create a date with a specific format? [duplicate]

I have the following doubt related how to create a format date in Java.

In a Java application I have to create a date (the value have to be the current date) formatted in this way: 2015-05-26 (yyyy-mm-dd)

So I know that I can obtain the current date simply build a new java.util.Date object, this way:

Date dataDocumento = new Date();

but how can I specify my date format?

Tnx

like image 243
AndreaNobili Avatar asked May 26 '15 09:05

AndreaNobili


1 Answers

Try like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date c= sdf.parse("2015-05-26");
String date=sdf.format(c);
System.out.println(date);

To format the current date in yyyy-MM-dd format you can try like this

Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd").format(date);

Kindly refer SimpleDateFormat

like image 68
Rahul Tripathi Avatar answered Oct 06 '22 00:10

Rahul Tripathi