Convert AM / PM date string from Oracle database

I have a timestamp on the form. 03-AUG-12 08.15.00.000000000 PM -05:00 I cannot get a view Stringon the form on yyyy-MM-dd HH:mm:ss.

Here is my code:

public static void convert() {

    String oldstring = "03-AUG-12 08.15.00.000000000 PM -05:00";
    Date date = null;
    try {
        date = new SimpleDateFormat("dd-MMM-yy HH.mm.ss.S aa").parse(oldstring);
    }
    catch (ParseException e) {
        e.printStackTrace();
    }

    String newstring = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    System.out.println(newstring);
}

This is mainly a timestamp with a timezone format from an Oracle database.

+3
source share
3 answers

You cannot use SimpleDateFormat to parse such a string, at least without any restrictions:

  • A timezone type of -05: 00 (in accordance with ISO 8601) is not supported until Java 7. Using Java 7, you can use the XXX pattern to parse it.

  • To correctly analyze the name of the month, you must indicate that you need English.

  • (S) . "08.15.00.100000000", SimpleDateFormat 8:15:00 100000000ms, 28 . , 0, .

Java 7, - :

new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa XXX", Locale.ENGLISH)

+4

:

        date = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa").parse(oldstring);

h, AM/PM 1-12.

+3

TL;DR

DateTimeFormatter f = new DateTimeFormatterBuilder ().parseCaseInsensitive ().appendPattern ( "dd-MMM-yy hh.mm.ss.SSSSSSSSS a ZZZZZ" ).toFormatter ().withLocale ( Locale.US );
OffsetDateTime odt = OffsetDateTime.parse ( "03-AUG-12 08.15.00.000000000 PM -05:00" , f );

,

Oracle. , . ResultSet java.sql.Timestamp .

java.sql.Timestamp ts = myResultSet.getTimestamp( … );

java.time. , , / java.time.

Instant instant = ts.toInstant();

, JDBC 4.2 Java 8 , java.time.Instant ResultSet::getObject.

, .

, Java, . java.time.

nanosecond, . , , , java.time .

java.time

. , ; ISO 8601 , .

"", . - , Aug, -.

String pattern = "dd-MMM-yy hh.mm.ss.SSSSSSSSS a ZZZZZ";
DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder ().parseCaseInsensitive ().appendPattern ( pattern );

, . Locale () , .. () , , , ..

Locale l = Locale.US;
DateTimeFormatter f = fb.toFormatter ().withLocale ( l );

, OffsetDateTime.

String input = "03-AUG-12 08.15.00.000000000 PM -05:00";
OffsetDateTime odt = OffsetDateTime.parse ( input , f );

. , 8 PM 24- 20 .

System.out.println ( "input: " + input + " | odt.toString(): " + odt );

: 03-AUG-12 08.15.00.000000000 PM -05: 00 | odt.toString(): 2012-08-03T20: 15-05: 00

A offset-from-UTC , (DST). ZoneId, ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );

java.time

java.time Java 8 . , java.util.Date, .Calendar java.text.SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru .

java.time Java 6 7 ThreeTen-Backport Android ThreeTenABP (. ...).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes, such as Interval, YearWeek, YearQuarter, etc.

+1
source

All Articles