Java 如何正确创建具有特定格式的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30454933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly create a date with a specific format?
提问by AndreaNobili
I have the following doubt related how to create a format date in Java.
我对如何在 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)
在 Java 应用程序中,我必须以这种方式创建一个日期(值必须是当前日期):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:
所以我知道我可以通过java.util.Date
这种方式获取当前日期,只需构建一个新对象:
Date dataDocumento = new Date();
but how can I specify my date format?
但如何指定我的日期格式?
Tnx
Tnx
采纳答案by Rahul Tripathi
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
要以 yyyy-MM-dd 格式格式化当前日期,您可以尝试这样
Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd").format(date);
Kindly refer SimpleDateFormat
回答by Jesper
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
".
java.util.Date
对象本身没有格式。它们只是时间戳值,就像 anint
只是一个数字,没有任何固有格式。因此,不存在“Date
具有格式的对象”这样的东西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
的String
。您可以使用SimpleDateFormat
一个转换Date
到一个String
特定格式。例如:
Date date = new Date();
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(date);
System.out.println(text);
回答by cн?dk
You have to use SimpleDateFormat:
你必须使用SimpleDateFormat:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
NoteThat MM
stands for month and not mm
.
注意这MM
代表一个月,而不是mm
。
And you format your date and parse it as a Date
object like this:
然后你格式化你的日期并将它解析为这样的Date
对象:
Date dt = sf.parse(sf.format(new Date()));
Using format(new Date())
you can format a new Date()
.
使用format(new Date())
您可以格式化一个new Date()
.
回答by Afsun Khammadli
Try following:
尝试以下操作:
String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
回答by BhandariS
Code
代码
import java.text.SimpleDateFormat; import java.util.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);
}
}
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
回答by BhandariS
Use the following-
使用以下-
String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
MoreOver You can use any of given formet if required-
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());
回答by Revathi Muthukrishnan
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));
}
}
回答by sampopes
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);