从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:45:03  来源:igfitidea点击:

Parsing date from Calendar in Java

javaparsingdatecalendar

提问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 Dateto 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 MMstands for months and mmfor minutes. See also SimpleDateFormatjavadoc.

请注意,MM代表月份和mm分钟。另请参阅SimpleDateFormatjavadoc

回答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-ssand 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 Calendarobject: The way to pull a Dateout of a Calendaris via the getTime()method.

你会很高兴地听到,有从来不需要解析从约会Calendar对象:拉的方式Date进行的Calendar是通过getTime()方法。



EDIT:

编辑:

To output the date in eDatein ISO style format:

eDateISO 样式格式输出日期:

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。