How can we convert java.util.Date to in Java?

0 votes

I am trying to use a java.util.Date as input and then creating a query with it - so I need a java.sql.Date. But, its not working. Is there any way to solve this problem?

Jun 29, 2018 in Java by Daisy
• 8,090 points

2 answers to this question.

0 votes
  • Use java.time classes instead of java.util.Date & java.sql.Date with JDBC 4.2 or later
  • Convert to/from java.time if inter-operating with code not yet updated to java.time
     ,                                         // Specify the ordinal number of which argument in SQL statement.
    myJavaUtilDate.toInstant()                  // Convert from legacy class `java.util.Date` (a moment in UTC) to a modern `java.time.Instant` (a moment in UTC).
        .atZone( ZoneId.of( "Africa/Tunis" ) )  // Adjust from UTC to a particular time zone, to determine a date. Instantiating a `ZonedDateTime`.
        .toLocalDate()                          // Extract a date-only `java.time.LocalDate` object from the date-time `ZonedDateTime` object.
answered Jun 29, 2018 by Sushmita
• 6,890 points
0 votes

It's easy to convert a java.util.Date object to java.sql.Timestamp in Java, all you need to do is call the getTime() method to get the long value from java.util.Date object and pass it to java.sql.Timestamp constructor, as shown below:

public Timestamp getTimestamp(java.util.Date date){
  return date == null ? null : new java.sql.Timestamp(date.getTime());
answered Aug 27, 2019 by Sirajul
• 55,680 points

Related Questions In Java

0 votes
2 answers
+15 votes
21 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 55,680 points
0 votes
1 answer

Can we convert a string to hexadecimal in java

I find this to be an easy ...READ MORE

answered Jul 4, 2018 in Java by Akrati
• 3,150 points
0 votes
2 answers

Can you give me the proper difference between java.util.Date vs java.sql.Date ?

The class represents a particular moment in time, with ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 55,680 points
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 55,680 points
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,890 points
0 votes
2 answers

How can we add the local JAR files to the Maven Project in Java?

Firstly I would like to give credit ...READ MORE

answered Nov 5, 2018 in Java by Sushmita
• 6,890 points
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,890 points