Reputation: 11
How can i fix the variable "time" for the gregorian calendar date before 1970. Or what was the unit of the variable "time" of gregorian calendar for the date before 1970?
I use hibernate for the object-relational mapping. And the data I'm trying to Save to my database is a date type gregorian calendar. But whenever the date is less than 1970, my application crash.
Upvotes: 1
Views: 4732
Reputation: 339561
ZonedDateTime.of(
LocalDate.of( 1969 , Month.DECEMEBER , 25 ) ,
LocalTime.NOON ,
ZoneId.of( "Africa/Tunis" )
).toInstant()
You have not presented enough information to diagnose your crash.
Beware that the date-time capabilities of various databases varies widely. The SQL standard barely touches on the topic of date-time handling, so little is required. Some databases have quite limited support. Any serious enterprise-oriented database should be able to easily store moments going many centuries both in the past as well as the future.
Use the java.time classes added to Java 8 and later. These types are apparently now supported in Hibernate (I’m not a user).
Internally, moments after the epoch of 1970-01-01T00:00Z (first moment of 1970 in UTC) are represented as a count of nanoseconds, a positive number. For moment before the epoch, a negative number of nanoseconds. But you should not really care. Just use the java.time classes as intended, and never see that count number.
If you were to want noon of Christmas Day in 1969 in New Zealand:
LocalTime lt = LocalTime.NOON ;
LocalDate ld = LocalDate.of( 1969 , Month.DECEMEBER , 25 ) ;
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
With JDBC 4.2 and later, you can directly exchange these java.time types with your database. No need for numbers or strings. The old java.sql.Timestamp
and related classes are now legacy, and can be forgotten.
Adjust your moment from its time zone to UTC, extract a Instant
. Same simultaneous moment, same point on the timeline, but viewed through the lens of the wall-clock time used by the people of a particular region.
Instant instant = zdt.toInstant() ;
Pass to your database for a column of type TIMESTAMP WITH TIME ZONE
.
myPreparedStatement.setObject( … , instant ) ;
And retrieval.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime zdt = instant.atZone( ZoneId.of( "America/Montreal" ) ) ;
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?
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.
Upvotes: 2
Reputation: 26733
Timestamps before Epoch (1970 Jan 1st) are represented by negative numbers. Have a look at this SO answer to see an example.
If your application "crashes" (whatever that means), you need to look how is it represented in the database and how is it mapped.
Upvotes: 2
Reputation: 3147
Cant you use Calender class?
public class DatePrint {
public static void main(String[] argv) {
Calendar c = new GregorianCalendar(1900, 10, 11);
System.out.println(c.get(Calendar.DAY_OF_MONTH) + " "
+ c.get(Calendar.MONTH) + ", " + c.get(Calendar.YEAR) + " "
+ c.get(Calendar.ERA));
}
}
Upvotes: 1