18

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

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    See the following: https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html – Afsun Khammadli May 26 '15 at 09:44
  • 1
    Date does not have a concept of a format, it's simply a container of the number of milliseconds since the Unix epoch. Use a DateFormat when you want to display the date value in a particular format – MadProgrammer May 26 '15 at 10:07
  • There are 8 outdated answers below. The correct and current answer in 2017 is `LocalDate.now(ZoneId.systemDefault()).toString()` (it was even a valid answer when the question was asked in 2015). You may supply a different time zone depending on your needs. – Ole V.V. May 03 '17 at 19:54

8 Answers8

28

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

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
9

You have to use SimpleDateFormat:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

Note That MM stands for month and not mm.

And you format your date and parse it as a Date object like this:

Date dt = sf.parse(sf.format(new Date()));

Using format(new Date()) you can format a new Date().

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
4

java.util.Date objects do not have a format by themselves. They are just timestamp values, just like an int is just a number, without any inherent format. So, there is no such thing as "a Date object with the format yyyy-MM-dd".

The format is determined at the moment you convert the Date to a String. You can use SimpleDateFormat to convert a Date to a String in a specific format. For example:

Date date = new Date();

DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(date);

System.out.println(text);
Jesper
  • 202,709
  • 46
  • 318
  • 350
4

Try following:

String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
Afsun Khammadli
  • 2,048
  • 4
  • 18
  • 28
3

Try this code:

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

public class GetCurrentDateTime {
    public static void main(String[] args) {     
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); // you can change the format as you like
        Date date = new Date();
        System.out.println(dateFormat.format(date));
    }
}
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
3

You can use simple date format class for this:

import java.text.SimpleDateFormat;
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
     Date dataDocumento = new Date();
     sdf.format(dataDocumento);
sampopes
  • 2,646
  • 1
  • 22
  • 34
2

Use the following-

String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());

MoreOver You can use any of given formet if required-

new SimpleDateFormat("dd/MM/yyyy").format(new Date());
new SimpleDateFormat("dd-MM-yy:HH:mm:SS").format(new Date());
new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z").format(new Date());
1

Code

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

/**
 *
 * Java program to show how to format date in Java using SimpleDateFormat
 * Examples. Java allows to include date, time and timezone information
 * while formatting dates in Java.
 *
 * @author http://java67.blogspot.com
 */
public class DateFormatExample {

    public static void main(String args[]) {

        // This is how to get today's date in Java
        Date today = new Date();

        //If you print Date, you will get un formatted output
        System.out.println("Today is : " + today);

        //formatting date in Java using SimpleDateFormat
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
        String date = DATE_FORMAT.format(today);
        System.out.println("Today in dd-MM-yyyy format : " + date);

        //Another Example of formatting Date in Java using SimpleDateFormat
        DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
        date = DATE_FORMAT.format(today);
        System.out.println("Today in dd/MM/yy pattern : " + date);

        //formatting Date with time information
        DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
        date = DATE_FORMAT.format(today);
        System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

        //SimpleDateFormat example - Date with timezone information
        DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");
        date = DATE_FORMAT.format(today);
        System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);

    } 

}

Output

Today is : Tue May 26 16:11:27 IST 2015

Today in dd-MM-yyyy format : 26-05-2015

Today in dd/MM/yy pattern : 26/05/15

Today in dd-MM-yy:HH:mm:SS : 26-05-15:16:11:316

Today in dd-MM-yy:HH:mm:SSZ : 26-05-15:16:11:316 +0530
BhandariS
  • 606
  • 8
  • 20