android java parse date from string

后端 未结 3 661
难免孤独
难免孤独 2020-12-12 08:06

In android application I\'m trying to parse Date from string to find difference between two times.

SimpleDateFormat dateFormatDateAndTime = new SimpleDateFor         


        
相关标签:
3条回答
  • 2020-12-12 08:28
     Log.v(TAG, "startDateTime: " + startDateTime + "stopDateTime: " + stopDateTime); 
    //Fri Oct 06 01:00:00 GMT+05:30 2017   Fri Jan 06 04:00:00 GMT+05:30 2017
    

    You output date object, not string. When you use this: "startDateTime: " + startDateTime + "stopDateTime: " + stopDateTime, java will call method toString() in stopDateTime and startDateTime objects.

    0 讨论(0)
  • 2020-12-12 08:34

    You need to change the second MM (months) to mm (minutes):

    SimpleDateFormat dateFormatDateAndTime = new SimpleDateFormat("ddMMyyyyHHmm");
    
    0 讨论(0)
  • 2020-12-12 08:46

    tl;dr

    LocalDateTime.parse( 
        "060420150134" , 
        DateTimeFormatter.ofPattern( "ddMMuuuuHHmm" )
    ).atOffset( ZoneOffset.UTC )
    

    Using java.time

    As the correct accepted Answer says, your formatting pattern is incorrect.

    Also, you are using the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. See below for Android info.

    The formatting pattern codes are similar but not exactly identical. So study the class doc for DateTimeFormatter.

    String input = "060420150134" ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMuuuuHHmm" );
    

    Your input string lacks any indicator of offset-from-UTC or time zone. So we parse as a LocalDateTime.

    LocalDateTime ldt = LocalDateTime.parse( input , f );
    

    If you know for certain the time zone or offset intended for this data, apply a ZoneId to get a ZonedDateTime or apply a ZoneOffset to get a OffsetDateTime. Apparently in your context UTC itself is intended.

    OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; 
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
提交回复
热议问题