0

I have a date object I constructed with:

    Calendar cal = new GregorianCalendar();
    cal.set(2013, 12, 1, 0, 0, 0);
    Date begin = new Date(cal.getTimeInMillis());

I am trying to use it in a query and I know the query takes the format like: 2008-10-29 14:56:59

When I print my date it looks like: Wed Jan 01 00:00:00 EST 2014

My question:

How do I convert the date format from Wed Jan 01 00:00:00 EST 2014 to 2008-10-29 14:56:59

9
  • 3
    Use SimpleDateFormat? Commented Apr 18, 2014 at 13:03
  • Thanks for the link, was just reading it. So would "yyyy-MM-dd HH:mm:ss" do the trick? Commented Apr 18, 2014 at 13:08
  • 1
    Yes, it will convert the date into the format you want. Commented Apr 18, 2014 at 13:09
  • 1
    @guilhebl Or use a java.sql.Timestamp with JDBC directly. Commented Apr 18, 2014 at 13:16
  • 1
    what @MarkRotteveel said. Regardless of your interface to JDBC, you should be binding variables to fixed sql statements, not concatenating strings - otherwise you're just asking for a sql injection vulnerability. Commented Apr 18, 2014 at 13:18

4 Answers 4

3

This should help:

Calendar cal = new GregorianCalendar();
cal.set(2013, 11, 1, 0, 0, 0);
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(new Date(cal.getTimeInMillis())));
  • 11 is DECEMBER
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the extra comment about December.
0

You can use simpleDateFormat class. I given simple example below.

Here you have to provide your required date format, Date object.

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date =null;    
date = dateformat.format(begin);
System.out.println(date);

Comments

0

Use this to get the format you want

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateInString = "pass you date string here";
Date date = sdf.parse(dateInString);

Hope this helps.

Comments

0

Use objects rather than strings when communicating with your database. For a date-time value, use a date-time object.

java.time

Avoid the troublesome old date-time classes including Calendar and GregorianCalendar. They are poorly-designed, flawed, and confusing. Among their troubles is toString methods that dynamically apply the JVM’s current default time zone when generating a string, meant to be helpful but turns out to be an anti-feature.

Instead, use the modern java.time classes.

If your JDBC driver is updated for JDBC 4.2 and later, use the java.time types directly rather than the java.sql types.

Alter your input string to comply with ISO 8601 format. Change that SPACE in middle to T.

String input = "2008-10-29 14:56:59".replace( " " , "T" ) ;

Parse as a LocalDateTime since your input lacks any indication of time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

Or specify the date-time value as integers.

LocalDateTime ldt = LocalDateTime.of( 2013 , 12 , 1 , 0 , 0 , 0 , 0 ) ;

If your database column is of type TIMESTAMP WITHOUT TIME ZONE, you are ready to query. Be sure you understand that without the context of a time zone or offset-from-UTC, such values do not represent a specific moment, and are not a point on the timeline, rather they are vague ideas about possible moments.

String sql = "SELECT * FROM tbl_ WHERE when_ >= ? ; " ; 
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , ldt ) ;  // Call `setObject` to pass a java.time object directly without converting into `java.sql.*` type.
ResultSet resultSet = ps.executeQuery() ;
…
LocalDateTime ldt = resultSet.getObject( … , LocalDateTime.class ) ;

If you had intended to track actual moments, specific points on the timeline, then you would be using TIMESTAMP WITH TIME ZONE as your database column type, and using Instant and ZonedDateTime java.time classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.