解析字符串日期到目前为止 [英] Parsing String date to date

查看:128
本文介绍了解析字符串日期到目前为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串,格式为YYYY-MM-dd HH :: mm:ss。每次我解析字符串我都错了日期。(Sun Dec 29 10:10:10 CET 2013​​)。代码如下:

I have a date String in this format "YYYY-MM-dd HH::mm:ss". Every time i parse the string i get wrong date.("Sun Dec 29 10:10:10 CET 2013"). The code is below:

DateFormat timeFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date startDate = timeFormat.parse("2014-08-10 10:10:10");
System.out.println(startDate);

这将返回Sun Dec 29 10:10:10 CET 2013​​。如何正确解析日期String?

This returns "Sun Dec 29 10:10:10 CET 2013". How to correctly parse the date String?

推荐答案

而不是使用 YYYY 您将需要使用 yyyy

Rather than using YYYY you will need to use yyyy.

Y 到WEEK_YEAR,而 y 指的是YEAR。 WEEK_YEAR是基于一年中的第一个整周开始的(根据设置而有所不同),因此略有(有时在很大程度上与YEAR不同)。

Y Refers to WEEK_YEAR whereas y refers to YEAR. WEEK_YEARs are based on when the first full week of a year starts (this differs depending on settings), and thus are slightly (and sometimes largely) different from that of YEAR.

请参阅javadoc:
http: //docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year

See the javadoc: http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year

这篇关于解析字符串日期到目前为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆