3

I have for example 30 users and for everyone i want to set vacation with random start_day and random end_day of vacation. I want to use Date, not LocalDate. If i must do with LocalDate this is the answer.

LocalDate date = LocalDate.now();

 List<VacationUser> collectVacationUser = allVacationUsers.stream()
      .map(user -> {
         if (inVacation()) {
            return new VacationUser(date.minusDays(ThreadLocalRandom.current().nextInt(1, 5)),
                                        date.plusDays(ThreadLocalRandom.current().nextInt(1, 5)));
         } else {
            return new VacationUser(user.getPrimaryEmail());
         }
       }).collect(toList());
        return collectVacationUser;
    }

I want to do this with Date, because in JSON date format with 'Date' is this "yyyy/mm/dd", in the other hand if I use LocalDate a format in JSON is something like this

"year":2018,"month":"AUGUST","era":"CE","dayOfMonth":16,"dayOfWeek":"THURSDAY","dayOfYear":228,"leapYear":false,"monthValue":8,"chronology":{"id":"ISO","calendarType":"iso8601"
BRRusev
  • 525
  • 2
  • 6
  • 12
  • 2
    You are changing the Date class because the JSON format comes out right. The better approach is to see how you can configure your JSON formatter so that it formats LocalDate differently. Are you using org.json.JSONObject? Can you instead use Gson or Jackson libraries? – Teddy Aug 14 '18 at 12:33
  • If you still want to do this, you can use java.util.Date by using java.util.Calendar for date arithmetic. – Teddy Aug 14 '18 at 12:33
  • 2
    While the conversion from `LocalDate` to `Date` goes through a number of steps, it’s not hard. – Ole V.V. Aug 14 '18 at 12:46
  • 1
    I don’t know which tool you are using for creating your JSON, but there are ways to produce JSON output like `yyyy/MM/dd` or (ISO 8601) `yyyy-MM-dd` from `LocalDate`. Use your search engine. I believe this will be a better solution than falling back on the outdated class. – Ole V.V. Aug 14 '18 at 12:48
  • 2
    Seriously: you are confusing stuff. You shouldnt ask how to do that with Date objects, but you should ask how to properly format a LocalDate as JSON. – GhostCat Aug 14 '18 at 12:50

1 Answers1

2

tl;dr

  • Use types appropriate to your values: LocalDate
  • Never use the terrible legacy class java.util.Date
  • Learn to use converters/adapters in your Java⇔JSON serialization framework
  • Use standard ISO 8601 formats for date-time values whenever possible

Details

Use appropriate types to represent your data values. For a date-only value, without a time-of-day and without a time zone, the appropriate type is LocalDate.

Never use java.util.Date. That terrible class was supplanted years ago by the java.time classes.

As for generating textual representations in JSON, that is an entirely separate issue.

JSON has very few data types and none of them are date-time related. So whatever JSON output you are getting for your LocalDate input is a function of your particular Java-to-JSON library you are using. You do not divulge what library, so we cannot provide further assistance.

I can tell you that there is an established practical international standard for representing date-time values: ISO 8601. I strongly suggest always using these standard formats when serializing your date-time values to text.

For a date-only value, the standard format is YYYY-MM-DD such as 2018-01-23.

The java.time classes use these standard formats by default when parsing/generating strings.

LocalDate.parse( "2018-01-23" ) ;

And:

myLocalDate.toString() 

2018-01-23


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • But, how can i do this in my code. My example is : I have date.now(), which return me data for today ( 16/08/2018 ) , if i run tomorow this it return me ( 17/08/2018 ) ... and in my lambda i set random data for start day ( from today to random minus 1 - 5 days) and end day ( from today to random plus 1 - 5 days). In my datebase for column startdate,enddate type is TIMESTAMP. How can use LocalDate.now() with your example. Thanks for answer :) – BRRusev Aug 16 '18 at 08:49
  • @BRRusev (A) `date.now()` does not return a date, it returns a date-time in UTC. When you say "run tomorrow" that would be tomorrow according to UTC, not tomorrow in your own time zone. (B) Now in this comment you are talking about moments (`TIMESTAMP` in database) but your Question only talks about dates, not date-with-time. If you edit your Question to get **very specific** about your details, then maybe I can edit my Answer. But I cannot keep on guessing as you change your story. Please put more care and effort into drafting your Question so as to not waste our time here. – Basil Bourque Aug 16 '18 at 08:55
  • Тhank you for helping with the answer above I've fixed myself end all is ok :) – BRRusev Aug 16 '18 at 09:01