1

i want to get the day of month of a date in java

i try getday() but it return day of week

     for (int i =0; i<count; i++) {

  series1.add(new Task(Names.get(i),  
Date.from(LocalDate.of(Dates.get(j).getYear(), Dates.get(j).getMonth(),Dates.get(j).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC)),  
Date.from(LocalDate.of(Dates.get(k).getYear(),Dates.get(k).getMonth(),Dates.get(k).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC))  
         ) 
         );

  j+=2;k+=2;

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    What is `Dates`? – azurefrog May 24 '19 at 21:20
  • 1
    Identify your classes. Is `Date` referring to `java.util.Date`? Maybe `java.sql.Date`? What is `Dates`? What does `Dates.get()` return? Voting to close as unclear, and no proper code shown. – Basil Bourque May 25 '19 at 01:12
  • How is `Dates.get(j).getDay()` not an answer to your Question? (Not that I know it is, just guessing, as you have not identified your classes.) – Basil Bourque May 25 '19 at 01:13
  • 2
    Welcome to Stack Overflow! We’d sure like to help. Only, as you can understand from the other comments, we feel that we cannot as your question stands at the moment. The first and most important thing you may do to help us help you is [create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Please also include desired result of that example end tell us how observed result differs. – Ole V.V. May 25 '19 at 03:22
  • 1
    In case you are using `java.util.Date` and/or `java.sql.Date` I recommend that you see if you can avoid that. Those classes are poorly designed and long outdated. `LocalDate` belongs to java.time, the modern Java date and time API, and it would be much better if you could work with that class alone (or other classes from java.time). – Ole V.V. May 25 '19 at 03:34
  • Possible duplicate of [Convert between LocalDate and sql.Date [duplicate\]](https://stackoverflow.com/questions/29750861/convert-between-localdate-and-sql-date) – Ole V.V. May 25 '19 at 03:36

2 Answers2

2

I looked into your previous Stack Overflow question for some context. Please don’t expect that Stack Overflow users will usually do this. They will not.

As I understand it, Task is org.jfree.data.gantt.Task from JFreeChart, and its constructor requires two java.util.Date arguments (start and end). You are getting java.sql.Date objects from your database.

While you cannot (reasonably) change JFreeChart, since JDBC 4.2 you can take advantage of getting modern LocalDate objects rather than old-fashioned java.sql.Date objects from your database:

    while (rs.next()) {
        String a = rs.getString("TITRE");
        LocalDate startDate = rs.getObject("DATE DEBUT Prévi", LocalDate.class);
        LocalDate endDate = rs.getObject("DATE FIN prévi", LocalDate.class);
        series1.add(new Task(a,  
                Date.from(startDate.atStartOfDay().toInstant(ZoneOffset.UTC)),
                Date.from(endDate.atStartOfDay().toInstant(ZoneOffset.UTC))  
                ) 
        );
    }

I found it simpler not to put your fetched data into separate lists and taking it out again before constructing the Task objects. In your code please do as you find best.

PS The getXxx methods of java.sql.Date and java.util.Date are deprecated because they work unreliably across time zones, so even if you couldn’t avoid those classes, never call those methods. If you do get a java.sql.Date from somewhere, the first thing to do is call its toLocalDate​ method to convert it to a modern LocalDate.

EDIT: You are doing very much in a single statement with nested method calls. I agree with the comment by Basil Bourque that this can be hard for a reader (and yourself?) to follow. I believe that this was also part of why people (including myself) didn’t understand your question. We suggest breaking it up. One example would be:

        Instant startInstant = startDate.atStartOfDay().toInstant(ZoneOffset.UTC);
        Date startUtilDate = Date.from(startInstant);
        // Similarly for end date

        Task newTask = new Task(a, startUtilDate, endUtilDate);
        series1.add(newTask);

It’s wordier, but each step is easy to follow now.

Instead of startDate.atStartOfDay().toInstant(ZoneOffset.UTC) I usually do startDate.atStartOfDay(ZoneOffset.UTC).toInstant(), but that’s a matter of taste or habit. The result is the same in both cases.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Do you want to pass a zone or offset to `atStartOfDay`? – Basil Bourque May 25 '19 at 04:33
  • Currently you are passing `ZoneOffset.UTC` to the wrong method, `toInstant`. That method does not take any arguments. – Basil Bourque May 25 '19 at 04:43
  • As for passing zone or offset to `atStartOfDay`, I suppose we don’t really know enough detail to know the intentions of the author. – Basil Bourque May 25 '19 at 04:45
  • 1
    I was thinking `ZondDateTime`, but now I see what you mean. The code here is correct, I see now, but I suggest breaking it up into more lines to make the types more obvious, P.S. Good work having figured this all out. – Basil Bourque May 25 '19 at 04:52
0

I am assuming Dates is a Map so Dates.get(key) returns a particular date stored in the map.

In this case you can simply use the java.util.Calendar to get the day of the month as an integer from your dates.

Date dt = Dates.get(key);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

Hope it helps!

Abhishek Roy
  • 73
  • 1
  • 3
  • 11
  • 2
    These terrible classes were supplanted years ago by the modern *java.time* classes defined by JSR 310. Suggesting their use in 2019 is poor advice. – Basil Bourque May 25 '19 at 01:10