java java日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7915475/
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
java Date and time format
提问by surya
how to format "2011-10-25T13:00:00Z" string into date and time
如何将“2011-10-25T13:00:00Z”字符串格式化为日期和时间
i used simple date format class
我使用了简单的日期格式类
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
but it only giving the date value. not time values
但它只给出日期值。不是时间值
please help me to solve this problem
请帮我解决这个问题
回答by aioobe
This is because "yyyy-MM-dd"
only mentions year (yyyy
), month (MM
) and date (dd
). Try adding hh:mm
if you want hours and minutes.
这是因为"yyyy-MM-dd"
只提到了年(yyyy
)、月(MM
)和日期(dd
)。hh:mm
如果您想要小时和分钟,请尝试添加。
Example:
例子:
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(sim.format(new Date())); // prints "2011-10-27 01:56"
The full documentation of the format-string and its parts is found here. The documentation includes this example:
可以在此处找到格式字符串及其部分的完整文档。该文档包括此示例:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
- 2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
- 2001-07-04T12:08:56.235-0700
Perhaps it's something like that you're looking for.
也许这就是您正在寻找的东西。
回答by Narendra Yadala
Use the format "yyyy-MM-dd'T'HH:mm:ss'Z'"
for parsing this date format. See the documentation of SimpleDateFormatfor more info. Code will look like this
使用格式"yyyy-MM-dd'T'HH:mm:ss'Z'"
解析此日期格式。有关更多信息,请参阅SimpleDateFormat的文档。代码看起来像这样
String dateStr = "2011-09-19T15:57:11Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Date date = new SimpleDateFormat(pattern).parse(dateStr);
回答by Mahesh
var date=new Date();
date.toString();
var t=date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate()+"T"+date.getHours()+":"+date.getMinutes()+"."+date.getSeconds();
var t;
output: 2016-1-22T23:20.48
输出:2016-1-22T23:20.48