Java Timestamp - How can I create a Timestamp with the date 23/09/2007?

后端 未结 8 1781

How can I create a Timestamp with the date 23/09/2007?

相关标签:
8条回答
  • 2020-11-28 21:02

    tl;dr

    java.sql.Timestamp.from (
        LocalDate.of ( 2007 , 9 , 23 )
                 .atStartOfDay( ZoneId.of ( "America/Montreal" ) )
                 .toInstant()
    )
    

    java.time

    Let’s update this page by showing code using the java.time framework built into Java 8 and later.

    These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They supplant the notoriously troublesome old date-time classes bundled with early versions of Java.

    In java.time, an Instant is a moment on the timeline in UTC. A ZonedDateTime is an Instant adjusted into a time zone (ZoneId).

    Time zone is crucial here. A date of September 23, 2007 cannot be translated to a moment on the timeline without applying a time zone. Consider that a new day dawns earlier in Paris than in Montréal where it is still “yesterday”.

    Also, a java.sql.Timestamp represents both a date and time-of-day. So we must inject a time-of-day to go along with the date. We assume you want the first moment of the day as the time-of-day. Note that this is not always the time 00:00:00.0 because of Daylight Saving Time and possibly other anomalies.

    Note that unlike the old java.util.Date class, and unlike Joda-Time, the java.time types have a resolution of nanoseconds rather than milliseconds. This matches the resolution of java.sql.Timestamp.

    Note that the java.sql.Timestamp has a nasty habit of implicitly applying your JVM’s current default time zone to its date-time value when generating a string representation via its toString method. Here you see my America/Los_Angeles time zone applied. In contrast, the java.time classes are more sane, using standard ISO 8601 formats.

    LocalDate d = LocalDate.of ( 2007 , 9 , 23 ) ;
    ZoneId z = ZoneId.of ( "America/Montreal" ) ;
    ZonedDateTime zdt = d.atStartOfDay( z ) ;
    Instant instant = zdt.toInstant() ;
    java.sql.Timestamp ts = java.sql.Timestamp.from ( instant ) ;
    

    Dump to console.

    System.out.println ( "d: " + d + " = zdt: " + zdt + " = instant: " + instant + " = ts: " + ts );
    

    When run.

    d: 2007-09-23 = zdt: 2007-09-23T00:00-04:00[America/Montreal] = instant: 2007-09-23T04:00:00Z = ts: 2007-09-22 21:00:00.0

    By the way, as of JDBC 4.2, you can use the java.time types directly. No need for java.sql.Timestamp.

    • PreparedStatement.setObject
    • ResultSet.getObject

    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)
  • 2020-11-28 21:10

    What about this?

    java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.0");
    
    0 讨论(0)
  • 2020-11-28 21:18

    A more general answer would be to import java.util.Date, then when you need to set a timestamp equal to the current date, simply set it equal to new Date().

    0 讨论(0)
  • 2020-11-28 21:19

    For completeness sake, also a solution with Joda-Time version 2.5 and its DateTime class:

    new Timestamp(new DateTime(2007, 9, 23, 0, 0, DateTimeZone.forID( "America/Montreal" )).getMillis())
    
    0 讨论(0)
  • 2020-11-28 21:23

    You could also do the following:

    // untested
    Calendar cal = GregorianCalendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 23);// I might have the wrong Calendar constant...
    cal.set(Calendar.MONTH, 8);// -1 as month is zero-based
    cal.set(Calendar.YEAR, 2009);
    Timestamp tstamp = new Timestamp(cal.getTimeInMillis());
    
    0 讨论(0)
  • 2020-11-28 21:27

    By Timestamp, I presume you mean java.sql.Timestamp. You will notice that this class has a constructor that accepts a long argument. You can parse this using the DateFormat class:

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = dateFormat.parse("23/09/2007");
    long time = date.getTime();
    new Timestamp(time);
    
    0 讨论(0)
提交回复
热议问题