How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss)

119,185

Solution 1

You do not need to use substring at all since your format doesn't hold that info.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fechaStr = "2013-10-10 10:49:29.10000";  
Date fechaNueva = format.parse(fechaStr);

System.out.println(format.format(fechaNueva)); // Prints 2013-10-10 10:49:29

Solution 2

A date-time object is not a String

The java.sql.Timestamp class has no format. Its toString method generates a String with a format.

Do not conflate a date-time object with a String that may represent its value. A date-time object can parse strings and generate strings but is not itself a string.

java.time

First convert from the troubled old legacy date-time classes to java.time classes. Use the new methods added to the old classes.

Instant instant = mySqlDate.toInstant() ;

Lose the fraction of a second you don't want.

instant = instant.truncatedTo( ChronoUnit.Seconds );

Assign the time zone to adjust from UTC used by Instant.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z );

Generate a String close to your desired output. Replace its T in the middle with a SPACE.

DateTimeFormatter f = DateTimeFormatter.ISO_LOCAL_DATE_TIME ;
String output = zdt.format( f ).replace( "T" , " " );
Share:
119,185

Related videos on Youtube

user2284257
Author by

user2284257

Updated on September 15, 2020

Comments

  • user2284257
    user2284257 over 3 years

    Well, I'm having a detail using a Date, because I'm getting an Object from my DataBase and in the variable "fecha" (date) from that same object I'm getting a java.sql.Timestamp, so the formatt is with miliseconds, but i don't want the miliseconds to appear. So i need to formatt the date that I'm receiving from my DB to a new date that doesn't have miliseconds.

    This is the object Factura:

    public class Factura  implements java.io.Serializable {
    
     private FacturaId id;
     ...
     private boolean activo;
     private Date fecha;
    }
    

    In the xml that is mapped to the DB I have this code for that variable "fecha":

    <property name="fecha" type="timestamp">
      <column length="19" name="fecha" not-null="true"/>
    </property>
    

    In the Database that column is fecha DATETIME.

    And when I get an object Factura from my DB I'm getting this kind of date 2013-10-10 10:49:29.0 but I want it without the .0 (miliseconds).

    I've tried this (factura is the Factura object):

    try {
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date fechaNueva = null;
       String fechaStr = factura.getFecha().toString();
       int tamaño = fechaStr.length()-2;
       fechaStr = fechaStr.substring(0, tamaño); //I get a string without the miliseconds
       fechaNueva = format.parse(fechaStr);
    } catch(ParseException ex) {
       ...
    }
    

    But fechaNueva is giving me Thu Oct 10 10:49:29 CDT 2013 and I only want 2013-10-10 10:49:29, can you help me, please?

    Thanks a lot, in advance.

  • user2284257
    user2284257 over 10 years
    Thanks, bug I forget to say that i want to put that new fecha fechaNueva to the same object... something like this: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fechaStr = "2013-10-10 10:49:29.10000"; Date fechaNueva = format.parse(fechaStr); factura.setFecha(fechaNueva); But just with Date fechaNueva = format.parse(fechaStr); I got the wrong format Thu Oct 10 10:49:29 CDT 2013 that i dont want...
  • Sajal Dutta
    Sajal Dutta over 10 years
    @user2284257 No, not wrong format. It just prints by default (check the toString method of Date class) that way and of course uses your local timezone as you didn't specify a locale. You need to format it the way you want it to print as I did inside the print statement. That is not a problem because no matter how you format for your printing needs, the value of fechaNueva is still the same.
  • user2284257
    user2284257 over 10 years
    Ok, something like SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); ? I have tried that Locale.ENGLISH but nothing. Do you know what Locale could help me? or how to formatt that date to have the formatt i want? Thanks a lot.
  • Sajal Dutta
    Sajal Dutta over 10 years
    @user2284257 Locale is for TimeZone, not for formatting. format method of SimpleDateFormat is what you need as I stated before and in my print statement. If you tried the code I provided, you should see the correct output as you stated in your question: 2013-10-10 10:49:29
  • user2284257
    user2284257 over 10 years
    Yes, that's right, as a String is correct, but how I put that correct format 2013-10-10 10:49:29 to my object Factura as a Date? this is the problem: factura.setFecha(fechaNueva);, what i have to do to fechaNueva to have it formatted to 2013-10-10 10:49:29?
  • user2284257
    user2284257 over 10 years
    It's because i use that Factura object in a TableModel, so its printing at the column fecha this: Thu Oct 10 10:49:29 CDT 2013
  • Sajal Dutta
    Sajal Dutta over 10 years
    I think you are having problem understanding the concept. You are saving the correct value when you do setFecha. When you are printing it in your TableModel, print it using the format you want. You can create other simple date formats for your printing needs. Just don't do System.out.print(fechaNueva) or fechaNueva.toString(). You need to format it before you print.