How to set 24-hours format for date on java?

前端 未结 8 1055
盖世英雄少女心
盖世英雄少女心 2020-12-15 03:19

I have been developing Android application where I use this code:

Date d=new Date(new Date().getTime()+28800000);
String s=new SimpleDateFormat(\"dd/MM/yyyy          


        
相关标签:
8条回答
  • 2020-12-15 03:56

    tl;dr

    The modern approach uses java.time classes.

    Instant.now()                                        // Capture current moment in UTC.
           .truncatedTo( ChronoUnit.SECONDS )            // Lop off any fractional second.
           .plus( 8 , ChronoUnit.HOURS )                 // Add eight hours.
           .atZone( ZoneId.of( "America/Montreal" ) )    // Adjust from UTC to the wall-clock time used by the people of a certain region (a time zone). Returns a `ZonedDateTime` object.
           .format(                                      // Generate a `String` object representing textually the value of the `ZonedDateTime` object.
               DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
                                .withLocale( Locale.US ) // Specify a `Locale` to determine the human language and cultural norms used in localizing the text being generated. 
           )                                             // Returns a `String` object.
    

    23/01/2017 15:34:56

    java.time

    FYI, the old Calendar and Date classes are now legacy. Supplanted by the java.time classes. Much of java.time is back-ported to Java 6, Java 7, and Android (see below).

    Instant

    Capture the current moment in UTC with the Instant class.

    Instant instantNow = Instant.now();
    

    instant.toString(): 2017-01-23T12:34:56.789Z

    If you want only whole seconds, without any fraction of a second, truncate.

    Instant instant = instantNow.truncatedTo( ChronoUnit.SECONDS );
    

    instant.toString(): 2017-01-23T12:34:56Z

    Math

    The Instant class can do math, adding an amount of time. Specify the amount of time to add by the ChronoUnit enum, an implementation of TemporalUnit.

    instant = instant.plus( 8 , ChronoUnit.HOURS );
    

    instant.toString(): 2017-01-23T20:34:56Z

    ZonedDateTime

    To see that same moment through the lens of a particular region’s wall-clock time, apply a ZoneId to get a ZonedDateTime.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

    ZoneId z = ZoneId.of( "America/Montreal" );
    ZonedDateTime zdt = instant.atZone( z );
    

    zdt.toString(): 2017-01-23T15:34:56-05:00[America/Montreal]

    Generate string

    You can generate a String in your desired format by specifying a formatting pattern in a DateTimeFormatter object.

    Note that case matters in the letters of your formatting pattern. The Question’s code had hh which is for 12-hour time while uppercase HH is 24-hour time (0-23) in both java.time.DateTimeFormatter as well as the legacy java.text.SimpleDateFormat.

    The formatting codes in java.time are similar to those in the legacy SimpleDateFormat but not exactly the same. Carefully study the class doc. Here, HH happens to work identically.

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ).withLocale( Locale.US );
    String output = zdt.format( f );
    

    Automatic localization

    Rather than hard-coding a formatting pattern, consider letting java.time fully localize the generation of the String text by calling DateTimeFormatter.ofLocalizedDateTime.

    And, by the way, be aware that time zone and Locale have nothing to do with one another; orthogonal issues. One is about content, the meaning (the wall-clock time). The other is about presentation, determining the human language and cultural norms used in presenting that meaning to the user.

    Instant instant = Instant.parse( "2017-01-23T12:34:56Z" );
    ZoneId z = ZoneId.of( "Pacific/Auckland" );  // Notice that time zone is unrelated to the `Locale` used in localizing.
    ZonedDateTime zdt = instant.atZone( z );
    
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
                                           .withLocale( Locale.CANADA_FRENCH );  // The locale determines human language and cultural norms used in generating the text representing this date-time object.
    String output = zdt.format( f );
    

    instant.toString(): 2017-01-23T12:34:56Z

    zdt.toString(): 2017-01-24T01:34:56+13:00[Pacific/Auckland]

    output: mardi 24 janvier 2017 à 01:34:56 heure avancée de la Nouvelle-Zélande


    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.

    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?

    • Java SE 8, Java SE 9, Java SE 10, 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 Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    Joda-Time

    Update: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

    Joda-Time makes this kind of work much easier.

    // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
    // import org.joda.time.*;
    // import org.joda.time.format.*;
    
    DateTime later = DateTime.now().plusHours( 8 );
    DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd/MM/yyyy HH:mm:ss" );
    String laterAsText = formatter.print( later );
    
    System.out.println( "laterAsText: " + laterAsText );
    

    When run…

    laterAsText: 19/12/2013 02:50:18
    

    Beware that this syntax uses default time zone. A better practice is to use an explicit DateTimeZone instance.

    0 讨论(0)
  • 2020-12-15 03:58
    Date d=new Date(new Date().getTime()+28800000);
    String s=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(d);
    

    HH will return 0-23 for hours.

    kk will return 1-24 for hours.

    See more here: Customizing Formats

    use method setIs24HourView(Boolean is24HourView) to set time picker to set 24 hour view.

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