org.joda.time.format.DateTimeFormatter.parseDateTime()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(181)

本文整理了Java中org.joda.time.format.DateTimeFormatter.parseDateTime()方法的一些代码示例,展示了DateTimeFormatter.parseDateTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter.parseDateTime()方法的具体详情如下:
包路径:org.joda.time.format.DateTimeFormatter
类名称:DateTimeFormatter
方法名:parseDateTime

DateTimeFormatter.parseDateTime介绍

[英]Parses a date-time from the given text, returning a new DateTime.

The parse will use the zone and chronology specified on this formatter.

If the text contains a time zone string then that will be taken into account in adjusting the time of day as follows. If the #withOffsetParsed() has been called, then the resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the resulting DateTime will have the zone of this formatter, but the parsed zone may have caused the time to be adjusted.
[中]从给定文本解析日期时间,返回新的日期时间。
解析将使用此格式化程序上指定的区域和时间顺序。
如果文本包含时区字符串,则在调整一天中的时间时将考虑该字符串,如下所示。如果调用了#withOffsetParsed(),则生成的DateTime将具有基于解析时区的固定偏移量。否则,生成的DateTime将具有此格式化程序的区域,但解析的区域可能导致时间被调整。

代码示例

代码示例来源:origin: stackoverflow.com

String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));

代码示例来源:origin: stackoverflow.com

String dateString = "2010-03-01T00:00:00-08:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime); // 2010-03-01T04:00:00.000-04:00

代码示例来源:origin: stackoverflow.com

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
  public static void main(String[] args) {
    String text = "2011-03-10T11:54:30.207Z";
    DateTimeFormatter parser = ISODateTimeFormat.dateTime();
    DateTime dt = parser.parseDateTime(text);

    DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
    System.out.println(formatter.print(dt));
  }
}

代码示例来源:origin: stackoverflow.com

import org.joda.time.*;
import org.joda.time.format.*;

class Test
{   
  public static void main(String[] args)
  {
    parse("2002-10-10T12:00:00-05:00");
    parse("2002-10-10T17:00:00Z");
  }

  private static final DateTimeFormatter XML_DATE_TIME_FORMAT =
    ISODateTimeFormat.dateTimeNoMillis();

  private static final DateTimeFormatter CHECKING_FORMAT =
    ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);

  static void parse(String text)
  {
    System.out.println("Parsing: " + text);
    DateTime dt = XML_DATE_TIME_FORMAT.parseDateTime(text);
    System.out.println("Parsed to: " + CHECKING_FORMAT.print(dt));
  }
}

代码示例来源:origin: stackoverflow.com

DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
DateTime dateTime = parser.parseDateTime("2016-03-23T21:12:23+04:00");
System.out.println(dateTime); // 2016-03-23T18:12:23.000+01:00

代码示例来源:origin: stackoverflow.com

DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm");
System.out.println(dt.parseDateTime("12/17/2017 23:10"));

代码示例来源:origin: stackoverflow.com

String s = "2014-01-15T14:23:50.026PST";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // abort

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-01-15T14:23:50.026PST" is malformed at "PST"
  at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
  at time.JodaTest8.main(JodaTest8.java:83)

代码示例来源:origin: stackoverflow.com

String s = "2014-01-15T14:23:50.026";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS");

DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // 2014-01-15T14:23:50.0260

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS");
Date date = sdf.parse(s);
System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.0026 (bad!)

代码示例来源:origin: stackoverflow.com

String string1 = "Oct 15, 2012 1:07:13 PM";
String string2 = "Oct 23, 2012 03:43:34 PM";

DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH);

DateTime dateTime1 = dtf.parseDateTime(string1);
DateTime dateTime2 = dtf.parseDateTime(string2);
Period period = new Period(dateTime1, dateTime2);

PeriodFormatter formatter = new PeriodFormatterBuilder()
  .appendYears().appendSuffix(" years ")
  .appendMonths().appendSuffix(" months ")
  .appendWeeks().appendSuffix(" weeks ")
  .appendDays().appendSuffix(" days ")
  .appendHours().appendSuffix(" hours ")
  .appendMinutes().appendSuffix(" minutes ")
  .appendSeconds().appendSuffix(" seconds ")
  .printZeroNever()
  .toFormatter();

String elapsed = formatter.print(period);
System.out.println(elapsed);

代码示例来源:origin: stackoverflow.com

String time1 = "26-NOV-01 12.00.00.000000000 PM -07:00";
String time2 = "26-NOV-01 12.00.00.000000000 PM -08:00";
DateTimeFormatter parser = DateTimeFormat.forPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS aa Z").withZoneUTC(); // joda time date time formatter instance with a common UTC timezone

System.out.println(parser.parseDateTime(time1)); // parse to date time - gives: 2001-11-26T19:00:00.000Z
System.out.println(parser.parseDateTime(time2)); // parse to date time - gives: 2001-11-26T20:00:00.000Z

System.out.println(parser.parseDateTime(time2).compareTo(parser.parseDateTime(time1))); // Comparing both the times here - gives: 1

代码示例来源:origin: stackoverflow.com

String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(date);
System.out.println(dateTime); // 2009-07-16T19:20:30-05:00

代码示例来源:origin: stackoverflow.com

String date1 = "Apr 2010";
String date2 = "Jan 2009";

DateTimeFormatter formatter = DateTimeFormat.forPattern("MMM yyyy");

DateTime d1 = formatter.parseDateTime(date1);
DateTime d2 = formatter.parseDateTime(date2);

Months monthsBetween = Months.monthsBetween(d1, d2);
System.out.println("Months diff: " + monthsBetween.get(DurationFieldType.months()));
Years yearsBetween = Years.yearsBetween(d1, d2);
System.out.println("Years diff: " + yearsBetween.get(DurationFieldType.years()));

代码示例来源:origin: com.yahoo.sql4d/Sql4Ddriver

public static void main(String[] args) {
  System.out.println(dateTimeFormat.parseDateTime("2014-05-25T16:45:49"));
  System.out.println(dateTimeAndTZFormat.parseDateTime("2014-05-25T16:45:49+00:00"));
  System.out.println(dateTimeAndTZFormat.parseDateTime("2014-05-25T16:45:49Z"));
  System.out.println(dateTimeWithSubSecFormat.parseDateTime("2014-05-25T16:45:49.100"));
  System.out.println(dateTimeWithSubSecAndTZFormat.parseDateTime("2014-05-25T16:45:49.000Z"));
}

代码示例来源:origin: stackoverflow.com

String input = "2014-04-10T00:00:00.000";
DateTimeZone timeZoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecondFraction().withZone( timeZoneMontréal );
DateTime dateTime = null;
try {
  dateTime = formatter.parseDateTime( input );
} catch ( IllegalArgumentException e ) {
  System.out.println( "Unexpected format of incoming date-time string: " + input + ". Exception: " + e ); // Handle exception for bad input.
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
  DateTimeFormatter formatterC = DateTimeFormat.forPattern("HH:mm dd M YY").withZone(DateTimeZone.forID("Europe/Copenhagen"));
  System.out.println(
    formatterC.parseDateTime("19:30 29 8 11")
  );
}

代码示例来源:origin: stackoverflow.com

String dateString = "2015-08-21 03:15:00+5:30";

String pattern = "yyyy-MM-dd HH:mm:ssZ";

DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);

DateTime dateTime = dtf.parseDateTime(dateString);

System.out.println(dateTime);

代码示例来源:origin: stackoverflow.com

String dateString = "2011-06-07T14:08:59.697-07:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime);

代码示例来源:origin: stackoverflow.com

DateFormat df = new SimpleDateFormat("HH:mm:ss");
String starttime="8:30:00";
String endtime="10:30:00";

DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime1 = fmt.parseDateTime(starttime);
DateTime dateTime2 = fmt.parseDateTime(endtime);

if (dateTime1.isBefore(dateTime2)){
  System.out.println("awesome");
}

代码示例来源:origin: stackoverflow.com

String text = "2011-10-02 18:48:05";
 String text2 = "2011-10-02 18:50:05";
 DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
 DateTime oldDate = formatter.parseDateTime(text);
 DateTime newDate = formatter.parseDateTime(text2);
 System.out.println(oldDate);
 System.out.println(newDate);
 Interval interval = new Interval(oldDate, newDate);
 System.out.println(interval);

代码示例来源:origin: stackoverflow.com

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object

// Create a new formatter with the pattern you want
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss");
String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern
System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in

相关文章