Android SimpleDateFormat问题
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Log.e(MY_DEBUG_TAG, "Output is "+ date.getYear() + " /" + date.getMonth() + " / "+ (date.getDay()+1));
正在输出
09-13 14:20:18.740: ERROR/GoldFishActivity(357): Output is 111 /8 / 3
问题是什么?
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Log.e(MY_DEBUG_TAG, "Output is "+ date.getYear() + " /" + date.getMonth() + " / "+ (date.getDay()+1));
Is out putting
09-13 14:20:18.740: ERROR/GoldFishActivity(357): Output is 111 /8 / 3
What is the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在
Date
类中使用的方法已被弃用。getYear()
返回的值是年份减去 1900 的结果,即2011 - 1900 = 111
。getDay()
返回星期几,而3 = Wednesday
。getDate()
返回月份中的日期,但这也已被弃用。您应该使用
Calendar
类。The methods you are using in the
Date
class are deprecated.getYear()
returns a value that is the result of subtracting 1900 from the year i.e.2011 - 1900 = 111
.getDay()
returns the day of the week and3 = Wednesday
.getDate()
returns the day of the month, but this too is deprecated.You should use the
Calendar
class instead.阅读javadoc of
java.util.Date< /code>
仔细。
getYear
返回自 1900 年以来的年数。getMonth
返回从 0 开始的月份(0 = 一月,1 = 二月等)。getDay
返回星期几(0 = 星期日,1 = 星期一等),而不是月份中的哪一天。所有这些方法都已被弃用。你不应该再使用它们了。
Read the javadoc of
java.util.Date
carefully.getYear
returns the number of years since 1900.getMonth
returns the month, starting from 0 (0 = January, 1 = February, etc.).getDay
returns the day of week (0 = Sunday, 1 = Monday, etc.), not the day of month.And all these methods are deprecated. You shouldn't use them anymore.
java.time
2014 年 3 月,Java 8 引入了现代的
java.time
日期时间 API,它取代了 容易出错的遗留java.util
日期时间 API。任何新代码都应使用java.time
API*。使用现代日期时间 API 的解决方案
由于现代日期时间 API 中使用的默认格式基于 ISO 8601 标准,您不需要显式使用
DateTimeFormatter
对象来解析符合 ISO 8601 标准的日期字符串。可以直接使用LocalDate#parse
来解析它。用于日志记录的格式化字符串:
您不需要获取单独的日期元素并将它们连接起来以获得用于日志记录的格式化字符串。您可以创建另一个具有所需格式的
DateTimeFormatter
对象,并格式化LocalDate
以获得所需的字符串,如下所示:演示:
输出:
在线演示
注意:无论出于何种原因,如果您需要的实例从
LocalDate
的这个对象的java.util.Date
中,您可以执行以下操作:从 跟踪:日期时间。
* 如果您收到
java.util.Date
对象,请使用Date#toInstant
,并派生其他来自其中的java.time
日期时间对象。java.time
In March 2014, Java 8 introduced the modern,
java.time
date-time API which supplanted the error-prone legacyjava.util
date-time API. Any new code should use thejava.time
API*.Solution using modern date-time API
Since the default formats used in the modern date-time API are based on ISO 8601 standards, you are not required to use a
DateTimeFormatter
object explicitly to parse a date string conforming to the ISO 8601 standards. You can directly useLocalDate#parse
to parse it.Formatted string for your logging:
You do not need to get individual date elements and join them to obtain a formatted string for your logging. You can create another
DateTimeFormatter
object with the required format and format theLocalDate
to get the required string as shown below:Demo:
Output:
Online Demo
Note: For whatever reason, if you need an instance of
java.util.Date
from this object ofLocalDate
, you can do so as follows:Learn more about the modern date-time API from Trail: Date Time.
* If you receive a
java.util.Date
object, convert it to ajava.time.Instant
object usingDate#toInstant
, and derive otherjava.time
date-time objects from it.