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

筅森魡賤 提交于 2019-11-29 13:13:16

问题


This question already has an answer here:

  • Extract time from date String 7 answers
  • return date type with format in java [duplicate] 1 answer

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


回答1:


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




回答2:


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().




回答3:


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);



回答4:


Try following:

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



回答5:


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());



回答6:


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);



回答7:


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));
    }
}



回答8:


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


来源:https://stackoverflow.com/questions/30454933/how-to-correctly-create-a-date-with-a-specific-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!