如何正确创建具有特定格式的日期?

How to correctly create a date with a specific format?

我对如何在 Java 中创建格式日期有以下疑问。

在 Java 应用程序中,我必须创建一个日期(值必须是当前日期),格式如下:2015-05-26 ( yyyy-mm-dd)

所以我知道我可以获得当前日期,只需构建一个新的 java.util.Date 对象,这样:

Date dataDocumento = new Date();

但是如何指定我的日期格式?

Tnx

这样试试:

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

要将当前日期格式化为 yyyy-MM-dd 格式,您可以这样尝试

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

请参考SimpleDateFormat

java.util.Date 对象本身没有格式。它们只是时间戳值,就像 int 只是一个数字,没有任何固有格式。所以,没有 "a Date object with the format yyyy-MM-dd".

这样的东西

格式是在您将 Date 转换为 String 时确定的。您可以使用 SimpleDateFormatDate 转换为特定格式的 String。例如:

Date date = new Date();

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

System.out.println(text);

你必须使用SimpleDateFormat:

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

注意 MM 代表月份而不是 mm

然后您格式化日期并将其解析为 Date 对象,如下所示:

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

使用 format(new Date()) 你可以格式化 new Date().

尝试以下操作:

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

代码

进口java.text.SimpleDateFormat; 导入 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);

    } 

}

输出

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

使用以下-

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

MoreOver 如果需要,您可以使用任何给定的格式-

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

试试这个代码:

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

您可以为此使用简单的日期格式 class:

import java.text.SimpleDateFormat;
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
     Date dataDocumento = new Date();
     sdf.format(dataDocumento);