Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:29:16+00:00 2026-06-03T15:29:16+00:00

I have a java.util.Date object, and I need to insert it into a datetime

  • 0

I have a java.util.Date object, and I need to insert it into a datetime field in MySQL in UTC format.

java.util.Date date = myDateFromSomewhereElse;
PreparedStatement prep = con.prepareStatement(
    "INSERT INTO table (t1, t2) VALUES (?,?)");

java.sql.Timestamp t = new Timestamp(date.getTime());
prep.setTimestamp(1, t, Calendar.getInstance(TimeZone.getTimeZone("PST"));
prep.setTimestamp(2, t, Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(prep.toString());

Which gives me the prepared SQL statement string:

INSERT INTO table (t1, t2) VALUES ('2012-05-09 11:37:08','2012-05-09 11:37:08');

The timestamp returned is the same timestamp regardless of the timezone I specify. It’s ignoring the Calendar object with timezone that I specify. What is going on and what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:29:18+00:00Added an answer on June 3, 2026 at 3:29 pm

    TimeZones are just different ways to view a date (which is a fixed point in time). I wrote a little example here (pay close attention to the assert):

    // timezone independent date (usually interpreted by the timezone of 
    // the default locale of the user machine)
    Date now = new Date();
    
    // now lets get explicit with how we wish to interpret the date
    Calendar london =  Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
    Calendar paris = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));
    
    // now set the same date on two different calendar instance
    london.setTime(now);
    paris.setTime(now);
    
    // the time is the same
    assert london.getTimeInMillis() == paris.getTimeInMillis();
    
    // London is interpreted one hour earlier than Paris (as of post date of 9th May 2012)
    String londonTime = london.get(Calendar.HOUR) + ":" + london.get(Calendar.MINUTE);
    String londonTZ = london.getTimeZone().getDisplayName(london.getTimeZone().inDaylightTime(london.getTime()), TimeZone.SHORT);
    System.out.println(londonTime + " " + londonTZ);
    
    // Paris is interpreted one hour later than Paris (as of post date of 9th May 2012)
    String parisTime = paris.get(Calendar.HOUR) + ":" + paris.get(Calendar.MINUTE);
    String parisTZ = paris.getTimeZone().getDisplayName(paris.getTimeZone().inDaylightTime(paris.getTime()), TimeZone.SHORT);
    System.out.println(parisTime + " " + parisTZ);
    

    The output to this snippet is (the result will be different depending on execution date/time):

    8:18 BST
    9:18 CEST
    

    Your snippet in the question is simply not doing anything with regard to the date being stored. Usually databases are configured for a native TimeZone. I advise storing an extra field representing the TimeZone to be used when interpreting the date.

    It is not (generally) a good idea to modify dates (which are essentially just milliseconds before/after a fixed point in time) as this would be a lossy modification that would be interpreted differently at different points in the year (due to daylight savings time).

    Or this : http://puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stored date (java.util.date object) in a DATETIME field of a Sqlite table.
On a JSTL/JSP page, I have a java.util.Date object from my application. I need
How can I set the localization so that java.util.Date have the German format? (
I need to create a java.util.Date object with an Australian timezone. this object is
I have string Tue Nov 12 2010,I want to parse it in java.util.Date object.
I have Date in this format (2012-11-17T00:00:00.000-05:00). I need to convert the date into
I have two Java instances of java.util.Date and I have to find out if
I have an application that passes in java.util.Date. I want to check whether this
I have a java.util.Set<City> cities and I need to add cities to this set
Let's say I have a java.util.Properties object. The Properties object has a method called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.