-1

No matter what I do its not working. I want to have it in dd/mm/yyyy I have tried and unable to get it done.Tried with JAVA 8 api of Instant localdate localtime localdatetime too.

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("23/09/2007");
long time = date.getTime();
Timestamp ts = new Timestamp(time);
System.out.println(ts);

Prints like this 2007-09-23 00:00:00.0;

  • 3
    a TimeStamp is never formatted, to have it formatted, you use a formatter with a pattern to turn it into a String. But since you start from a String, why don't you just keep that String, instead of the entire parsing thing? – Stultuske Apr 16 '20 at 07:39
  • I need to use setTimestamp method in ResultSet and it asks for a timestamp. I want to store it in db the format I wish to. Ofcouse I can do setString but I want to try to use setTimestamp and it hasnt been working. – Naveen Kumar Apr 16 '20 at 07:41
  • 1
    Either you want to store a date/time, or you want to store it formatted. Again: dates, or time(stamps) are not formatted. The formatting is done by the developer, while formatting it into a String – Stultuske Apr 16 '20 at 07:42
  • thank you I need to move on to java 8 classes and be as it is unless very much require date format patterns. – Naveen Kumar Apr 16 '20 at 08:05
  • I recommend you don’t use `Timestamp`. That class is poorly designed and long outdated. Instead of `setTimestamp()` use `setObject()` and pass it an `OffsetDateTime` or a `LocalDateTime`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And don’t worry about its format, it will work. I furthermore certainly recommend you stay away from `SimpleDateFormat`, it’s a notorious troublemaker of a class. – Ole V.V. Apr 16 '20 at 12:45
  • 1
    Does this answer your question? [Formatting timestamp in Java](https://stackoverflow.com/questions/23692117/formatting-timestamp-in-java). See also [my answer here](https://stackoverflow.com/a/52485250/5772882) (it’s about `Date` for the sake of the example, but as mentioned at the end of it, it also goes for `Timestamp`). – Ole V.V. Apr 16 '20 at 12:51
  • `Timestamp` apart from being obsolete is also the wrong type for a date without time of day (and apparently without time zone). Use `LocalDate`. And store it into a column of type `date` in your database. – Ole V.V. Apr 16 '20 at 12:58
  • See: [*Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2*](https://stackoverflow.com/q/43039614/642706) – Basil Bourque Apr 16 '20 at 22:42

2 Answers2

1

TimeStamp has its own format, so you need to format it as per your needs

Try,

      SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
      Date date = f.parse("23/12/2007 00:00:00");
      String strDate = f.format(date);
      System.out.println("Current Date = "+strDate);
Shubham
  • 549
  • 4
  • 11
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Apr 16 '20 at 22:43
1

java.time

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes.

➥ Never use java.util.Date, java.sql.Date, nor java.sql.Timestamp.

For a date only, without time-of-day and without time zone, use LocalDate.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
String input = "23/09/2007" ;
LocalDate ld = LocalDate.parse( input , f ) ;

Generate text in standard ISO 8601 format.

String output = ld.toString() ;

Generate text in that same custom format.

String output = ld.format( f ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154