用Java将“ 2010年1月2日”格式的String
转换为Date
的最佳方法是什么? html
最终,我想将月份,日期和年份分解为整数,以即可以使用 java
Date date = new Date(); date.setMonth().. date.setYear().. date.setDay().. date.setlong currentTime = date.getTime();
将日期转换为时间。 api
并且,某些客户端技术(例如GWT )不提供SimpleDateFormat。 安全
选择Calendar.getInstance()是个好主意,您的要求是比较两个日期。 长时间约会。 oracle
String str_date = "11-June-07"; DateFormat formatter; Date date; formatter = new SimpleDateFormat("dd-MMM-yy"); date = formatter.parse(str_date);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date; try { date = dateFormat.parse("2013-12-4"); System.out.println(date.toString()); // Wed Dec 04 00:00:00 CST 2013 String output = dateFormat.format(date); System.out.println(output); // 2013-12-04 } catch (ParseException e) { e.printStackTrace(); }
这对我来讲能够。 框架
尽管某些答案在技术上是正确的,但不建议这样作。 ide
有关Joda-Time的一些注意事项以下。 spa
在Joda-Time中 , DateTime对象真正知道其本身分配的时区。 这与java.util.Date类造成了对比,后者彷佛有一个时区,但没有。 线程
请注意,在下面的示例代码中,咱们如何将时区对象传递给解析字符串的格式化程序。 该时区用于将日期时间解释为发生在该时区中。 所以,您须要考虑并肯定该字符串输入所表明的时区。 设计
因为输入字符串中没有时间部分,所以Joda-Time会将指定时区中一天的第一时刻指定为一天中的时间。 因为夏令时(DST)或其余异常,一般这表示00:00:00
但并不是老是如此。 顺便说一句,您能够经过调用withTimeAtStartOfDay
对任何DateTime实例执行相同的withTimeAtStartOfDay
。
在Joda-Time中,格式化程序模式中使用的字符与java.util.Date/Calendar中的字符类似,但并不彻底相同。 仔细阅读文档。
咱们一般在Joda-Time中使用不可变的类。 而不是修改现有的Date-Time对象,咱们调用的方法是基于另外一个对象建立一个新的新实例,并复制了大多数方面,除非须要进行更改。 下面的最后一行是对withZone
的调用。 不变性有助于使Joda-Time很是线程安全,还可使某些工做更加清晰。
您将须要java.util.Date对象,以与其余不了解Joda-Time对象的类/框架一块儿使用。 幸运的是,来回移动很是容易。
从java.util.Date对象(此处称为date
)到Joda-Time DateTime…
org.joda.time.DateTime dateTime = new DateTime( date, timeZone );
从Joda-Time转到java.util.Date对象的另外一方向…
java.util.Date date = dateTime.toDate();
String input = "January 2, 2010"; java.util.Locale locale = java.util.Locale.US; DateTimeZone timeZone = DateTimeZone.forID( "Pacific/Honolulu" ); // Arbitrarily chosen for example. DateTimeFormatter formatter = DateTimeFormat.forPattern( "MMMM d, yyyy" ).withZone( timeZone ).withLocale( locale ); DateTime dateTime = formatter.parseDateTime( input ); System.out.println( "dateTime: " + dateTime ); System.out.println( "dateTime in UTC/GMT: " + dateTime.withZone( DateTimeZone.UTC ) );
运行时...
dateTime: 2010-01-02T00:00:00.000-10:00 dateTime in UTC/GMT: 2010-01-02T10:00:00.000Z
使用Java 8,咱们得到了一个新的日期/时间API( JSR 310 )。
可使用如下方式在Java 8中解析日期,而无需依赖Joda-Time :
String str = "January 2nd, 2010";
// if we 2nd even we have changed in pattern also it is not working please workout with 2nd DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM Q, yyyy", Locale.ENGLISH); LocalDate date = LocalDate.parse(str, formatter);
// access date fields int year = date.getYear(); // 2010 int day = date.getDayOfMonth(); // 2 Month month = date.getMonth(); // JANUARY int monthAsInt = month.getValue(); // 1
LocalDate是用于表示日期(无时间)的标准Java 8类。 若是要解析包含日期和时间信息的值,则应使用LocalDateTime 。 对于具备时区的值,请使用ZonedDateTime 。 二者都提供相似于LocalDate
的parse()
方法:
LocalDateTime dateWithTime = LocalDateTime.parse(strWithDateAndTime, dateTimeFormatter); ZonedDateTime zoned = ZonedDateTime.parse(strWithTimeZone, zoneFormatter);
DateTimeFormatter Javadoc中的列表格式字符:
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The following pattern letters are defined: Symbol Meaning Presentation Examples ------ ------- ------------ ------- G era text AD; Anno Domini; A u year year 2004; 04 y year-of-era year 2004; 04 D day-of-year number 189 M/L month-of-year number/text 7; 07; Jul; July; J d day-of-month number 10 Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter Y week-based-year year 1996; 96 w week-of-week-based-year number 27 W week-of-month number 4 E day-of-week text Tue; Tuesday; T e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T F week-of-month number 3 a am-pm-of-day text PM h clock-hour-of-am-pm (1-12) number 12 K hour-of-am-pm (0-11) number 0 k clock-hour-of-am-pm (1-24) number 0 H hour-of-day (0-23) number 0 m minute-of-hour number 30 s second-of-minute number 55 S fraction-of-second fraction 978 A milli-of-day number 1234 n nano-of-second number 987654321 N nano-of-day number 1234000000 V time-zone ID zone-id America/Los_Angeles; Z; -08:30 z time-zone name zone-name Pacific Standard Time; PST O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; Z zone-offset offset-Z +0000; -0800; -08:00;