Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java parse date with optional timezone

public class DateFormatTest {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "yyyy-MM-dd[[ ]['T']HH:mm[:ss][XXX]]");

    private TemporalAccessor parse(String v) {
        return formatter.parseBest(v,
                                   ZonedDateTime::from,
                                   LocalDateTime::from,
                                   LocalDate::from);
    }

    @Test public void testDateTime1() {
        assertEquals(LocalDateTime.of(2014, 9, 23, 14, 20, 59),
                     parse("2014-09-23T14:20:59"));
    }

    @Test public void testDateTime2() {
        assertEquals(LocalDateTime.of(2014, 9, 23, 14, 20),
                     parse("2014-09-23 14:20"));
    }

    @Test public void testDateOnly() {
        assertEquals(LocalDate.of(2014, 9, 23), parse("2014-09-23"));
    }

    @Test public void testZonedDateTime() {
        assertEquals(ZonedDateTime.of(2014, 9, 23, 14, 20, 59, 0,
                                      ZoneOffset.ofHoursMinutes(10, 30)),
                     parse("2014-09-23T14:20:59+10:30"));
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: java herencia 
Java :: Java Target annotations attributes 
Java :: could not find or load main class org.apache.catalina.startup.bootstrap caused by: java.lang.classnotfoundexception: org.apache.catalina.startup.bootstrap 
Java :: setBackgrounfTint color to relative layout from java file 
Java :: java param.ExStyle |= 0x08000000; 
Java :: at org.apache.spark.sql.errors.QueryCompilationErrors$.failedToFindKafkaDataSourceError(QueryCompilationErrors.scala:1037) 
Java :: is overriding only works with inherited methods? 
Java :: session-less control using java 
Java :: okhttp Sending and Receiving Network Requests 3 21 
Java :: vibration android studio 
Java :: Duplicate entry Exception 
Java :: how to write a java program for printing child or adult in java 
Java :: fail if condition not met in java if condition 
Java :: cannot write data document seems to have been closed already in java 
Java :: open external hostservices url in javafx 
Java :: tutorialedge working with docker 
Java :: android java sqlite alter table 
Java :: Determining what type of data the ArrayList will contain in java. 
Java :: convert text file into binnary format bitmap using java 
Java :: Java Protected Access Modifier package one 
Java :: system out java quick 
Java :: object class java 
Java :: how to get individual words from a string in java 
Java :: how to set current item for spinner android 
Java :: Get the number of weeks between two dates 
Java :: java string start with substring 
Java :: @runwith junit 5 
Java :: what is java steam 
Java :: graph with dependies problem 
Sql :: ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =