difference between util.date and sql.date? [duplicate] - java

This question already has answers here:
Difference between Date class in Package java.util & Package java.sql
(5 answers)
java.util.Date vs java.sql.Date
(7 answers)
Closed 9 years ago.
Can many one help me?
import java.util.Date;
public class DateDemo {
public static void main(String [] p)
{
java.util.Date date1 = new Date();
#SuppressWarnings("deprecation")
java.sql.Date date2 = new java.sql.Date(2013, 12, 22);
System.out.println(date1.compareTo(date2));
}
}

As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
java.sql.Date just represent DATE without time information while java.util.Date represent both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date.
In order to suppress time information and to confirm with definition of ANSI SQL DATE type, the millisecond values used in java.sql.Date instance must be "normalized by setting the hours, minutes, seconds and milliseconds to zero in the timezone with with DATE instance is associated. In other words all time related information is removed from java.sql.Date class.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBWDwBD0

Date from util package has is combination of date and time while Date from SQL package only represent only Date part. to be precise Date contains year, month and day information while Time means hour, minute and second information. java.util.Date contains all year, month, day, hour, minute and second information. In fact java.sql.Time and java.sql.TimeStamp which represents TIME and TIMESTAMP type of SQL database is more close to java.util.Date, It extends java.util.DATE and if you are using java.util.DATE in your Class to represent DATE value its better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBmF4lBd

Related

How to save date to oracle [duplicate]

This question already has answers here:
Java Date - Insert into database
(10 answers)
Closed 7 years ago.
I want to save date to oracle from eclipse java program.
Right now I am using this code
DateFormat dt=new SimpleDateFormat("MM/dd/yyyy");
java.sql.Date dob=(java.sql.Date)dt.parse("02/02/2015");
ob.setDateOfBirth(dob);
The table has a column named Date_of_birth having date datatype.
But i am getting an error
Exception in thread "main" java.lang.ClassCastException:
java.util.Date cannot be cast to java.sql.Date
at com.TestCustomerDao.main(TestCustomerDao.java:22)
Please Help
The class that DateFormat.parse(String) returns is java.util.Date.
You need a java.sql.Date, which is actually a subclass of the above. You can only cast an object up to a class it inherits from, you can't cast down to a class that inherits from it.
In order to do that properly, you need to create a new java.sql.Date object from the java.util.Date object by using:
java.sql.Date dob = new java.sql.Date( dt.parse("02/02/2015").getTime() );
This gets the internal time stamp (representation of time as milliseconds since January 1970) from the java.util.Date object, and creates a java.sql.Date that is based on the same time stamp.

Convert java.time.LocalDateTime SE 8 to timestamp [duplicate]

This question already has answers here:
Convert LocalDate to LocalDateTime or java.sql.Timestamp
(7 answers)
Closed 4 years ago.
How do you convert a Localdatetime to timestamp? I want to use the new SE 8 date api because it is better than the util date and calendar. I plan to use localdatetime throughout my program and then place that date into a mysql database. I have looked for an answer but there doesn't seem to be very many questions and answers for java.time. This is a little of the code that I am testing. This is as far as I got.
LocalDateTime c = LocalDateTime.now();
java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.getLong());
I think I need to convert it into a long first, but I don't know how. The api allows for converting individual elements like month and day, but not for the whole date. Since I'm already here, how do you convert back from timestamp? Should I just use jodatime?
I tried this:
LocalDateTime c = LocalDateTime.now();
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("this:" + c);
java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.atZone(zoneId).toEpochSecond());
pst.setTimestamp(2, javaSqlDate);
This only saves the date around 1970. The system.print prints the current date correctly. I want it to save the current date.
LocalDateTime l = LocalDateTime.now();
Timestamp t = Timestamp.valueOf(l);
Source: https://coderanch.com/t/651936/databases/Convert-java-time-LocalDateTime-SE
First of all, you should decide if you really want to use LocalDateTime.
Below are some explanations about the difference, taken from here:
<...> LocalDateTime is not a point on the time line as Instant is, LocalDateTime is just a date and time as a person would write on a note. Consider the following example: two persons which were born at 11am, July the 2nd 2013. The first was born in the UK while the second in California. If we ask any of them for their birth date it will look that they were born on the same time (this is the LocalDateTime) but if we align the dates on the timeline (using Instant) we will find out that the one born in California is few hours younger than the one born in the UK (NB: to create the appropriate Instant we have to convert the time to UTC, this is where the difference lays).<...>
In order to get long from Instant you could use getEpochSecond() method.
In order to get long from LocalDateTime you should provide a timezone.

Convert between LocalDate and sql.Date [duplicate]

This question already has answers here:
How to convert LocalDate to SQL Date Java?
(3 answers)
Closed 7 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
What's the correct way to convert between java.sql.Date and LocalDate (in both directions) in Java 8 (or higher)?
The Java 8 version (and later) of java.sql.Date has built in support for LocalDate, including toLocalDate and valueOf(LocalDate).
To convert from LocalDate to java.sql.Date you can use
java.sql.Date.valueOf( localDate );
And to convert from java.sql.Date to LocalDate:
sqlDate.toLocalDate();
Time zones:
The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the above conversions, the results depend on the system's default timezone (as pointed out in the comments).
If you don't want to rely on the default timezone, you can use the following conversion:
Date now = new Date();
LocalDate current = now.toInstant()
.atZone(ZoneId.systemDefault()) // Specify the correct timezone
.toLocalDate();

Joda LocalDate To LocalDateTime [duplicate]

This question already has answers here:
Convert Joda LocalDate or java.util.Date to LocalDateTime having time at Start of the Day
(2 answers)
Closed 5 years ago.
I am trying to convert Joda LocalDate to Joda LocalDateTime, for that I am using the method toLocalDateTime(LocalTime.MIDNIGHT), as of now it is working
fine for example: for the given joda Localdate 2025-02-28 I getting the expected joda LocalDateTime 2025-02-28T00:00:00.000, But my fear is, whether this method works fine at all situation. For example during dayLight saving time zone anomalies..etc..
Update: I did a small research on this question, here it is
toLocalDateTime(LocalTime time) Documentation saying that: Converts LocalDate object to a LocalDateTime with LocalTime to fill in the missing fields.
As I initializing LocalTime with LocalTime.MIDNIGHT, from here LocalTime.MIDNIGHT is a static final field initialized to new LocalTime(0, 0, 0, 0);, you can see that it is a time values are hardcode to zero values with ISOChronology getInstanceUTC(), so I think I will get the required output without any issue.
From the documentation, we know that
LocalDate is an immutable datetime class representing a date without a time zone.
LocalDateTime is an unmodifiable datetime class representing a datetime without a time zone.
Internally, LocalDateTime uses a single millisecond-based value to represent the local datetime. This value is only used internally and is not exposed to applications.
Calculations on LocalDate are performed using a Chronology. This chronology will be set internally to be in the UTC time zone for all calculations.
We also know that the toLocalDateTime method of LocalDate class is implemented like this:
public LocalDateTime toLocalDateTime(LocalTime time) {
if (time == null) {
throw new IllegalArgumentException("The time must not be null");
}
if (getChronology() != time.getChronology()) {
throw new IllegalArgumentException("The chronology of the time does not match");
}
long localMillis = getLocalMillis() + time.getLocalMillis();
return new LocalDateTime(localMillis, getChronology());
}
Considering also that UTC has no Daylight saving time, we can conclude that you don't have to fear daylight saving problems nor time zone anomalies using the toLocalDateTime method because this method dont deal with timezones.

Setting timestamp to Calendar in java

I am using following code to set SQL timestamp in calendar instance, it is working fine. Is this correct?
TimeStamp expireDate= ab.getUExpireDate();
Calendar cal = Calendar.getInstance();
cal.setTime(expireDate); // ← this line
You can check the difference in util.Date and sql.Date in the following post
java.util.Date vs java.sql.Date
You can import:
1)java.sql.Timestamp
***The biggest difference between java.sql.Date and java.sql.Timestamp is that the java.sql.Date only keeps the date, not the time, of the date it represents. So, for instance, if you create a java.sql.Date using the date and time 2014-12-24 21:20, then the time (21:20) would be cut off. If you use a java.sql.Timestamp then the time is kept.
method to add timeStamp object in Calender is :
2) cal.setTime(expireDate);

Categories

Resources