Search code examples
javaandroiddateandroid-calendar

How to cast date and time in Android


In my application i want get some data from server and i should cast date and time!
I receive date and time with this format end_date: "2020-04-08 13:11:14" from server.

I want get now date and time from my device and to calculate with above date (end_date), if this time under 24h i should show for example 15 hour later, but if this time more than 24h i should show 2 days later!

But i don't know how can i it?
Can you help me with send code or send to me other tutorials?!
I searched that but I didn't find anything.


Solution

  • java.time

    The modern approach uses java.time classes.

    Table of date-time types in Java, both modern and legacy

    Parse the input string as a LocalDateTime.

    To parse, replace the SPACE in the middle with a T to comply with ISO 8601 standard.

    String input = "2020-04-08 13:11:14".replace( " " , "T" ) ;
    LocalDateTime ldt = LocalDateTime.parse( input ) ;
    

    Your input lacks an indicator of any time zone or offset-from-UTC. So we do not know if this was meant to be 1 PM in Tokyo Japan, 1 PM in Toulouse France, or 1 PM in Toledo Ohio US. So you cannot reliably compare this to the current date and time.

    If you want to presume this string was meant to tell time in your time zone, then assign a time zone to get a ZonedDateTime.

    ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
    ZonedDateTime then = ldt.atZone( z ) ;
    

    Capture the current moment in the same zone.

    ZonedDateTime now = ZonedDateTime.now( z ) ;
    

    Calculate 24 hours later.

    ZonedDateTime twentyFourHoursFuture = now.plusHours( 24 ) ;
    

    Compare.

    boolean within24Hours = then.isBefore( twentyFourHoursFuture ) ;
    

    Determine elapsed time using the Duration class.

    Duration duration = Duration.between( then , now ) ;
    

    If you want to trust the JVM’s current default time zone, call ZoneId.systemDefault. Beware that this default can be changed by other Java code during execution of your app.

    ZoneId z = ZoneId.systemDefault() ;
    

    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.

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

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

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    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.