Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set random days on users with Date in Java?

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"
like image 965
BRRusev Avatar asked Mar 09 '26 07:03

BRRusev


1 Answers

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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 122
Basil Bourque Avatar answered Mar 11 '26 00:03

Basil Bourque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!