如何解析日期?
我正在尝试使用 SimpleDateFormat
解析此日期,但它不起作用:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String[] args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
如果我使用 strDate="2008-10-14"
尝试此代码,我会得到肯定的答案。 有什么问题? 我该如何解析这种格式?
附言。 我从 jDatePicker
获取此日期,但没有说明如何修改用户选择日期时获得的日期格式。
I am trying to parse this date with SimpleDateFormat
and it is not working:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String[] args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
If I try this code with strDate="2008-10-14"
, I have a positive answer. What's the problem? How can I parse this format?
PS. I got this date from a jDatePicker
and there is no instruction on how modify the date format I get when the user chooses a date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是您的日期格式如下:
但使用的是
SimpleDateFormat
即:两种格式不一致。 您需要构造一个
SimpleDateFormat
与您尝试解析为日期的字符串的布局相匹配。 将内容排列起来以便于查看,您需要一个像这样的SimpleDateFormat
:检查我链接到的 JavaDoc 页面并查看如何使用字符。
The problem is that you have a date formatted like this:
But are using a
SimpleDateFormat
that is:The two formats don't agree. You need to construct a
SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want aSimpleDateFormat
like this:Check the JavaDoc page I linked to and see how the characters are used.
我们现在有一种更现代的方法来完成这项工作。
java.time
java.time 框架与 Java 捆绑在一起8 及以后。 请参阅教程。 这些新类的灵感来自 Joda-Time,由 JSR 310,并由 ThreeTen-Extra 扩展项目。 它们比麻烦的旧类 java.util.Date/.Calendar 等有了巨大的改进。
请注意,像
EDT
这样的 3-4 个字母代码既不是标准化的也不是唯一的。 尽可能避免它们。 学习使用 ISO 8601 标准格式。 java.time 框架可能会尝试翻译,但许多常用代码都有重复的值。顺便说一下,请注意 java.time 默认情况下如何使用 ISO 8601 格式生成字符串,但通过在括号中附加时区名称进行扩展。
转储到控制台。
运行时。
调整时区
为了好玩,让我们调整到印度时区。
转换为 juDate
如果您确实需要 java.util.Date 对象与尚未更新为 java.time 类型的类一起使用,请转换。 请注意,您将失去指定的时区,但同一时刻会自动调整为UTC。
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like
EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
Dump to console.
When run.
Adjust Time Zone
For fun let's adjust to the India time zone.
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
getSelectedDate 怎么样? ? 无论如何,特别是在您的代码问题上,问题出在这一行:
构造函数中的字符串必须与日期的格式匹配。 有关如何执行此操作的文档位于此处 。 看起来您需要类似于“EEE MMM d HH:mm:ss zzz yyyy”的内容
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
回应:
“How to conversion Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de veranocentral (México)) to dd-MM-yy in Java?”,它被标记为多么重复
试试这个:
使用 java.util.Date、java.text.SimpleDateFormat,这是一个简单的解决方案。
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With
java.util.Date
,java.text.SimpleDateFormat
, it's a simple solution.您不能期望使用使用不同格式设置的 SimpleDateFormat 来解析日期。
要解析您的“Thu Jun 18 20:56:02 EDT 2009”日期字符串,您需要一个像这样的 SimpleDateFormat (大致):
使用它将字符串解析为日期,然后使用其他 SimpleDateFormat 将该日期转换为您的格式想。
JavaDoc: http://docs.oracle.com/javase /7/docs/api/java/text/SimpleDateFormat.html
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html