3

The following code prints out "3920-06-02", but it's supposed to be "2020-05-02". What did I do wrong?

import java.sql.Date
// ...

public static void main(String[] args) {
    Date May0220 = new Date(2020,  5,  2);
    System.out.println(May0220.toString());
}

I want to use java.sql and not java.util.

12
  • 6
    Don't use that constructor. It's been deprecated for 15 years now. Commented Mar 14, 2020 at 16:30
  • 1
    You probably want a local date without time. If so use java.time.LocalDate. Is there any specific reason you want to use java.sql.Date? Commented Mar 14, 2020 at 16:31
  • 6
    use LocalDate like this LocalDate May0220 = LocalDate.of(2020, 5, 2); Commented Mar 14, 2020 at 16:33
  • 3
    You can also create java.sql.Date from LocalDate: Date sqlDate = Date.valueOf(May0220) Commented Mar 14, 2020 at 16:54
  • 1
    LocalDate works very nicely with SQL date. Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2. You don’t want java.sql.Date. It’s poorly designed (you haven’t seen but a slight bit of its problems) and long outdated. Commented Mar 15, 2020 at 11:50

2 Answers 2

2

It seems as if java.sql.Date; uses the year from 1900, and uses array like indexing for the month, so it's off one less than what it should be.

This is what I did to get your desired result.

public static void main(String[] args) 
{
    Date May0220 = new Date(2020 - 1900, 5 - 1, 2);
    System.out.println(May0220+"");
}

A more general form would be

{
    int year = 2020;
    int month = 5;
    Date date = new Date(year - 1900, month - 1, 2);
    System.out.println(date+"");
}

Good Luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I was thinking of doing that but I thought there must be a more elegant way of doing it. Thanks!
@ChrisRahmé Most JDBC drivers now support java.time.LocalDate using setObject() and getObject(). java.time.LocalDate is always local, while java.sql.Date is world time; they represent two different things. If your app runs in a different time zone than your database you'll see wrong conversions.
1

You can use this reference How to Get Current Date and Time in Java to get the output like you expected . I've Just modified the code in the example by removing the time part.

The librararies are java.time.format.DateTimeFormatter; and java.time.LocalDateTime.

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;  
public class PrintDateTime {  
  public static void main(String[] args) {  
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDateTime now = LocalDateTime.now();
    System.out.println(dtf.format(now));
 }  
}  

Hope you find it useful !

1 Comment

Thanks for showing the modern solution! Still simpler would be System.out.println(LocalDate.now(ZoneId.systemDefault()));.

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.