从 Java 日历中解析日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3246862/
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
Parsing date from Calendar in Java
提问by Vishal
I am having following function
我有以下功能
public static Date parseDate(String date, String format) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(date);
}
I am using this as follows in my code
我在我的代码中使用如下
Calendar eDate = Calendar.getInstance();
eDate.add(Calendar.DAY_OF_MONTH,10);
Date date = null;
try {
date = parseDate(eDate.getTime().toString(),"yyyy-MM-dd hh-mm-ss");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But it is throwing -
但它正在抛出 -
java.text.ParseException: Unparseable date
What is the problem here?
这里有什么问题?
采纳答案by BalusC
The format is not stored in the Date
. It is stored in the String
. The Date#toString()
returns a fixedformat which is described in its Javadoc.
格式不存储在Date
. 它存储在String
. 所述Date#toString()
返回一个固定其以其格式描述的Javadoc。
Do the formatting only at the momentyou need to display a Date
to a human as a String
.
仅在您需要Date
将 a 作为String
.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 10);
Date date = calendar.getTime();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(formattedDate);
Note that MM
stands for months and mm
for minutes. See also SimpleDateFormat
javadoc.
请注意,MM
代表月份和mm
分钟。另请参阅SimpleDateFormat
javadoc。
回答by Jon Skeet
You're currently formattingwith the default format from java.util.Date
, and then parsingwith a potentially different format. You should also change your format string - it's currently using a 12 hour clock with no am/pm indicator, and minutes twice. I think you mean: "yyyy-MM-dd HH-mm-ss"
您目前的格式与默认格式java.util.Date
,然后解析与潜在不同的格式。您还应该更改您的格式字符串 - 它当前使用的是 12 小时制,没有 am/pm 指示器,并且两次使用分钟。我想你的意思是:“yyyy-MM-dd HH-mm-ss”
回答by Christopher Klewes
You're inserting a Zulu Timestamp (UNIX), getTime()
returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
. Then you define the format as yyyy-mm-dd hh-mm-ss
and try to parse the timestamp with this pattern. Which doesn't match.
您正在插入 Zulu Timestamp (UNIX),getTime()
返回自January 1, 1970, 00:00:00 GMT
. 然后将格式定义为yyyy-mm-dd hh-mm-ss
并尝试使用此模式解析时间戳。哪个不匹配。
You could use Date date = calendar.getTime();
and then format it via new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
您可以使用Date date = calendar.getTime();
然后通过new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
回答by Carl Smotricz
You'll be happy to hear that there's never a need to parse a date from a Calendar
object: The way to pull a Date
out of a Calendar
is via the getTime()
method.
你会很高兴地听到,有从来不需要解析从约会Calendar
对象:拉的方式Date
进行的Calendar
是通过getTime()
方法。
EDIT:
编辑:
To output the date in eDate
in ISO style format:
以eDate
ISO 样式格式输出日期:
final DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String formattedDate = isoFormat.format(eDate.getTime());
That's untested, but I think it should work.
这是未经测试的,但我认为它应该有效。
回答by Bozho
Don't use toString()
for anything like that. toString()
should be used only for debug messages.
不要toString()
用于类似的事情。toString()
应该仅用于调试消息。
Use DateFormat.format(..)
to produce a string in a predictable form.
用于DateFormat.format(..)
以可预测的形式生成字符串。
回答by Eyal Schneider
you can simply use the date returned by the calendar, instead of transforming it into string and back into a date (apparently using a wrong date format). The date can be obtained by:
您可以简单地使用日历返回的日期,而不是将其转换为字符串并重新转换为日期(显然使用错误的日期格式)。日期可以通过以下方式获得:
eDate.getTime()
There seems to be no need for SimpleDateFormat in your case.
在您的情况下似乎不需要 SimpleDateFormat 。
回答by Chris Cudmore
Check the Date.toString() method.
检查 Date.toString() 方法。
The api states that it returns it in the format:
api 声明它以以下格式返回它:
dow mon dd hh:mm:ss zzz yyyy
which is:
即:
Mon Jan 28 14:22:07 EST 2004
You are telling the parser to expect: 2004-01-28 14-22-07
您告诉解析器期望: 2004-01-28 14-22-07
回答by CodeClimber
eDate.getTime().toString()
returns a String representation of a date in this format:
以这种格式返回日期的字符串表示形式:
dow mon dd hh:mm:ss zzz yyyy (see the java.util.Date API).
dow mon dd hh:mm:ss zzz yyyy(请参阅 java.util.Date API)。
You are trying to parse a date using this format:
您正在尝试使用以下格式解析日期:
yyyy-mm-dd hh-mm-ss .
yyyy-mm-dd hh-mm-ss 。
The code is correctly throwing the ParseException.
代码正确抛出 ParseException。