2

Scenario

User can record their license using the application, they enter the day the license was redeemed and they also input the length of the license(days).

I am trying to figure out if you are able to calculate a new date from both these variables. for example

days = INPUT FROM USER;          days = 7;      
dateRedeemed = new Date();       date = 24/06/2019;      
newDate = dateRedeemed + days;   newDate = 01/07/2019;    
//Getting the values
            String name = txtName.getText();
            String contact = txtContact.getText();
            int years = Integer.parseInt(cboyears.getSelectedItem().toString());
            int months = Integer.parseInt(cboMonths.getSelectedItem().toString());
            int days = Integer.parseInt(cboDays.getSelectedItem().toString());


//Calculation            
            days = (years * 365) + (months * 12) + days;
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
            Date dRedeemed = cboDate.getDate();
            String strRedeemed = format.format(dRedeemed);

If any one could help that would be great

Edit

Some great advice in this thread, Alot of you have been pointing out that the Date class in very bad and outdated i'm now going to start using the LocalDateTime Class as that seems more powerful, Another thing i would like to ask, is there a more efficient date picker. I have been using a swingx date picker is there a more effective choice?

Ryan MacPhee
  • 73
  • 1
  • 1
  • 8

3 Answers3

3

Java (any) solution:

Date currentDate = new Date(); // or any date you set
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DAY_OF_MONTH, 7);
Date currentDatePlusSevenDays = c.getTime();

Java 8+ solution:

Date currentDate = new Date(); // or any date you set
LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
localDateTime = localDateTime.plusDays(7);
Date currentDatePlusSevenDays = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

Java (any) solution with external library Joda-Time:

DateTime currentDate = DateTime.now(); // or any date you set
DateTime currentDatePlusSevenDays = currentDate.plusDays(7);   
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • `LocalDateTime` is the **wrong class** to be using here, as it cannot represent a moment. The simple solution is: `LocalDate.now().plusDays( 7 )` – Basil Bourque Jun 24 '19 at 23:18
  • @BasilBourque I'm not quite sure I got your comment right, maybe you'd consider to edit my answer keeping in mind your logic? – Sergey Emeliyanov Jun 25 '19 at 10:37
  • As for my Comment, ponder the difference between (A) saying “noon on the 23rd” in general and (B) saying “noon on the 23rd in Tokyo” or “noon on the 23rd in Montréal”. As for a solution, the author is apparently only interested in the date, not the time of day, and not a moment. So the proper solution is simply `LocalDate.now( ZoneId.of( "Africa/Casablanca" ) ).plusDays( 7 )` – Basil Bourque Jun 25 '19 at 18:21
0

If you use Java 8, you should make use of the LocalDateTime class.

It makes things so much easier.

LocalDateTime dateRedeemed = LocalDateTime.now(); // 2019-06-24T16:31:16.993
// custom date also possible (year, month, day (of month), hours, minutes, seconds and nanoseconds)
LocalDateTime dateRedeemed = LocalDateTime.of(2019, 06, 24, 13, 55, 36, 123);

// Add 7 days
LocalDateTime newDate = dateRedeemed.plusDays(7);
System.out.println(newDate.toString()); // 2019-07-01T16:31:16.993
Patze
  • 859
  • 10
  • 19
  • I am using a jxDatePicker so the user can pick the date which it was redeemed how would i parse that through LocalTimeDate Tried LocalDateTime dateRedeemed = cboDate.getDate(); but gave the errorDate 'cant be converted to LocalDateTime.' – Ryan MacPhee Jun 24 '19 at 14:42
  • 1
    `LocalDateTime` is exactly the **wrong class** to use for the current moment. Lacking any concept of time zone or offset-from-UTC, this class **cannot represent a moment** as explained in its Javadoc. I cannot think of any situation where calling `LocalDateTime.now()` is the right thing to do. For a moment, use `Instant`, `OffsetDateTime`, or `ZonedDateTime`. – Basil Bourque Jun 24 '19 at 14:52
0

If you don't use Java 8 you can use below code

int days = 8;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, days);

SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
Date newDate = c.getTime();
System.out.println(dateFormat.format(newDate));
Planck Constant
  • 1,406
  • 1
  • 17
  • 19
  • 2
    For Java 6 and Java 7, it would be much better to use the back-port of *java.time* found in the [*ThreeTen-Backport*](https://www.threeten.org/threetenbp/) project. The `Date` and `Calendar` classes are truly terrible, and should be avoided. – Basil Bourque Jun 24 '19 at 14:45
  • @BasilBourque I totally agree. I would recommend https://www.joda.org/joda-time/ from my side. Unfortunately many people/projects still use old and terrible `Date` and `Calendar` classes – Planck Constant Jun 24 '19 at 14:53
  • 1
    The *Joda-Time* project is now in maintenance mode, and recommends migrating to the *java.time* classes. Both projects are led by the same man, Stephen Colebourne. – Basil Bourque Jun 24 '19 at 14:56
  • Good to know! Thanks! – Planck Constant Jun 24 '19 at 15:00