Set Date in a single line

前端 未结 8 2109
一个人的身影
一个人的身影 2021-01-31 01:55

According to the Java API, the constructor Date(year, month, day) is deprecated. I know that I can replace it with the following code:

Calendar myCa         


        
相关标签:
8条回答
  • 2021-01-31 02:16

    This is yet another reason to use Joda Time

    new DateMidnight(2010, 3, 5)
    

    DateMidnight is now deprecated but the same effect can be achieved with Joda Time DateTime

    DateTime dt = new DateTime(2010, 3, 5, 0, 0);
    
    0 讨论(0)
  • 2021-01-31 02:18

    You could use

    new SimpleDateFormat( "yyyyMMdd" ).parse( "20100520" )
    
    0 讨论(0)
  • 2021-01-31 02:22

    You could use new GregorianCalendar(theYear, theMonth, theDay).getTime():

    public GregorianCalendar(int year, int month, int dayOfMonth)

    Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

    0 讨论(0)
  • 2021-01-31 02:22

    Calendar has a set() method that can set the year, month, and day-of-month in one call:

    myCal.set( theYear, theMonth, theDay );
    
    0 讨论(0)
  • 2021-01-31 02:24

    tl;dr

    LocalDate.of( 2015 , Month.JUNE , 7 )  // Using handy `Month` enum. 
    

    …or…

    LocalDate.of( 2015 , 6 , 7 )  // Sensible numbering, 1-12 for January to December. 
    

    java.time

    The java.time framework built into Java 8 and later supplants the troublesome old classes, java.util.Date/.Calendar.

    The java.time classes use immutable objects. So they are inherently thread-safe. You will have none of the thread-safety problems mentioned on the other answers.

    LocalDate

    This framework included a class for date-only objects without any time-of-day or time zone, LocalDate. Note that a time zone (ZoneId) is necessary to determine a date.

    LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
    

    You can instantiate for a specific date. Note that month number is a sensible range of 1-12 unlike the old classes.

    LocalDate localDate = LocalDate.of( 2015 , 6 , 7 );
    

    Or use the enum, Month.

    LocalDate localDate = LocalDate.of( 2015 , Month.JUNE , 7 );
    

    Convert

    Best to avoid the old date-time classes. But if you must, you can convert. Call new methods added to the old classes to facilitate conversions.

    In this case we need to specify a time-of-day to go along with our date-only value, to be combined for a java.util.Date object. First moment of the day likely makes sense. Let java.time determine the time of that first moment as it is not always 00:00:00.0.

    We also need to specify a time zone, as the date varies by time zone.

    ZoneId zoneId = zoneId.of( "America/Montreal" );
    ZonedDateTime zdt = localDate.atStartOfDay( zoneId );
    

    An Instant is a basic class in java.time, representing a moment on the timeline in UTC. Feed an Instant to static method on Date to convert.

    Instant instant = zdt.toInstant();
    java.util.Date utilDate = java.util.Date.from( instant );
    

    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, Java SE 11, and later - 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
      • Most 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….

    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)
  • 2021-01-31 02:30
    Date date = new DateTime(2014, 6, 20, 0, 0).toDate();
    

    DateTime is from Joda.org https://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html

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