简体   繁体   English

解析特定日期字符串格式

[英]Parse specific date string format

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

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? 如果不是,有没有一个很好的方法来做这个没有太多的代码?

Thanks in advance! 提前致谢!

  1. Use the SimpleDateFormat class to parse a date from a String to a Date instance. 使用SimpleDateFormat类将日期从String解析为Date实例。

     String date = "20130516T090000"; SimpleDateFormat x = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); Date d = x.parse(date); 

    There was a problem in your SimpleDateFormat format String, text in the format has to be quoted using single quoted (') to avoid interpretation. 您的SimpleDateFormat格式字符串存在问题,格式中的文本必须使用单引号(')引用以避免解释。

  2. Use the Calendar class to get the part of the date you want. 使用Calendar类获取所需日期的一部分。

     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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM