问题描述
我已经阅读了很多问题,并且在互联网上搜索了很多库,但我找不到能够快速完成这些的库。
I have read a lot of questions and searched for a lot of libs all over the internet but I can't find one that can do this quickly.
我想要以特定日期格式解析特定日期,例如:
I want to parse a specific date in a specific date format like this:
String date = "20130516T090000";
SimpleDateFormat x = new SimpleDateFormat("yyyyMMddTHHmmss");
String theMonth = x.parse(date, "M"); // 05
String theMonth = x.parse(date, "MMM"); // MAY
String theMinute = x.parse(date, "mm"); // 00
String theYear = x.parse(date, "yyyy"); // 2013
就这么简单。设置解析规则,特定日期格式以及检索每个我想要的数据(Month,Minute,Year ....)的方法
Just simple as that. A way to set a Parse Rule, a Specific Date Format and a way to retrieve each data i want (Month, Minute, Year....)
好的图书馆做得很好这个?如果是的话,你能一起举个例子吗?
Is there a good library to do EXACTLY this? If yes could you put an example together? If no, is there a good way to do this without much code?
提前感谢!
推荐答案
-
使用
SimpleDateFormat
类解析/ code>到
日期
实例。
String date = "20130516T090000";
SimpleDateFormat x = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date d = x.parse(date);
您的 SimpleDateFormat
格式有一个问题
使用日历$
Use the Calendar
class to get the part of the date you want.
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String theYear = String.valueOf(cal.get(Calendar.YEAR));
String theMonth = String.valueOf(cal.get(Calendar.MONTH));
String theMinute = String.valueOf(cal.get(Calendar.MINUTE));
这篇关于解析特定的日期字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!