1

I have a:

 String fromUser = "2002-10-29";

I would like to output it to Date type (Java.sql.Date) without dashes, and just numbers.

20021029 // yyyyMMdd

How do I achieve it? Thank you,

I've tried using DateFormat, SimpleDateFormat and most of them give me back yyyy-MM-dd.

The documentation in Oracle for Java.util.Date mentioned that the Constructor

Date(int year, int month, int day) 

is already deprecated.

Ken
  • 21
  • 2
  • First, you drop using `java.sql.Date` since it is poorly designed and long outdated, nothing you will want to struggle with. Use `LocalDate`. Second, you are asking the impossible. When you output a Java object, you are iplicitly calling its `toString` method. The `toString` methods of both `LocalDate` and `java.sql.Date` produce strings with hyphens in the format you saw, `yyyy-MM-dd`. – Ole V.V. Nov 04 '22 at 10:06
  • You can get any format you want, but only in a `String`. For example `yourLocalDate.format(DateTimeFormatter.BASIC_ISO_DATE)` will give you `20021029` (provided that the date is Oct 29, 2002). – Ole V.V. Nov 04 '22 at 10:07
  • 2
    I understand that you want to convert a string to an instance of `java.sql.Date`. If I understand correctly, then an instance of `java.sql.Date` **does not have a format**. The format is only relevant when you want to convert the `java.util.Date` object to a string. Similarly, for your database, a column in a table whose data type is DATE also **does not have a format**. The format is only used when displaying the value stored in the column. So **forget the format**. Are you trying to update a DATE column in your database using [JDBC](https://docs.oracle.com/javase/tutorial/jdbc/index.html) ? – Abra Nov 04 '22 at 10:27
  • @Abra Thanks, if it does not have a format, then I guess just by converting a String into Java.sql.Date type will suffice to insert into a DB. – Ken Nov 04 '22 at 11:40
  • @OleV.V. Thanks but I'm unable to change the whole database just because something is outdated. May be for future projects I will keep it in mind. – Ken Nov 04 '22 at 11:40
  • It doesn’t require any database change, only a change in Java. And most likely only in the code that you are modifying anyway (if the old class is used elsewhere in the code, you can let that be for now if you want). See also [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Nov 04 '22 at 12:18
  • `LocalDate.parse(fromUser).format(DateTimeFormatter.BASIC_ISO_DATE)`. – Ole V.V. Nov 04 '22 at 12:24

1 Answers1

1

Here are three ways to convert a string to java.sql.Date.

  1. Since the string is already in the recognized format, use [static] method valueOf(String) in class java.sql.Date.
  2. Convert the string to a LocalDate and call [static] method valueOf(LocalDate) also in class java.sql.Date.
  3. Convert the string to a date, get the epoch milliseconds and call the constructor of class java.sql.Date.

The below code demonstrates each of the above ways.

import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class JdbcTest {

    public static void main(String[] args) {
        String fromUser = "2002-10-29";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
        LocalDate ld = LocalDate.parse(fromUser, dtf);
        LocalDateTime ldt = ld.atStartOfDay();
        ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
        Instant inst = zdt.toInstant();
        long millis = inst.toEpochMilli();
        Date d = new Date(millis);
        System.out.println("Constructor: " + d);
        d = Date.valueOf(ld);
        System.out.println("From LocalDate: " + d);
        d = Date.valueOf(fromUser);
        System.out.println("From String: " + d);
    }
}

When I run the above code using JDK 19 on Windows 10, I get the following:

Constructor: 2002-10-29
From LocalDate: 2002-10-29
From String: 2002-10-29
Abra
  • 19,142
  • 7
  • 29
  • 41
  • `java.sql.Date.valueOf(LocalDate)` (and the other way round) is imho the way to go because it is the direct legacy compatibility of those two libraries. – deHaar Nov 04 '22 at 12:17
  • If you indispensably need to use `java.sql.Date`, for example for interoperating with old code, then I agree with @deHaar. I still don’t have enough information to know whether this is necessary at all, though. – Ole V.V. Nov 04 '22 at 12:28
  • @OleV.V. Oracle JDBC driver still maps database DATE data-type to `java.sql.Date` class, so new code also needs to use class `java.sql.Date`. Refer to chapter 11 [Accessing and Manipulating Oracle Data](https://docs.oracle.com/en/database/oracle/oracle-database/21/jjdbc/accessing-and-manipulating-Oracle-data.html#GUID-02FC3ADD-B0C6-4B9D-9320-0AB722906B05) of _JDBC Developer's Guide and Reference_ (21c, dated September 2022). I believe other DBMS's do the same. – Abra Nov 04 '22 at 12:57
  • I didn’t read that well enough. I have deleted my comment. The other option, the way I understand your link among other sources, is using some Oracle specific types, which is harmful to portability and may or may not also have some advantages. – Ole V.V. Nov 05 '22 at 07:09
  • @OleV.V. _the way I understand your link ... is using some Oracle specific types_ Not at all. Oracle JDBC driver maps `DATE` [database] data type to Java class `java.sql.Date`. By the way, SQL Server does the same. No proprietary types involved at all. – Abra Nov 05 '22 at 09:48