1

I am trying to setup a membership system, and want to implement a start and end date. I think I have successfully implemented the starting date, however, I am having a tough time with the expiration date (1 year later). The expiry should be stored in a text field which is a String, thus it needs to be converted. However, on compilation, I am presented with a NullPointerException.

center.add(createLabel("Membership Date Created"));
center.add(memDateCreatedTxtFld);
SimpleDateFormat sdf = new SimpleDateFormat ("E yyyy/MM/dd");
Date date = new Date();
memDateCreatedTxtFld.setText(sdf.format(date));

center.add(createLabel("Membership Date Expiry"));
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, 1);
Date nextYear = cal.getTime();
String reportDate = sdf.format(nextYear);
memDateExpiryTxtFld.setText(reportDate);
center.add(memDateExpiryTxtFld);

For example, since today is 2019/02/01, the expiry should be on 2020/02/01.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • https://alvinalexander.com/java/simpledateformat-convert-date-to-string-formatted-parse – PM 77-1 Feb 01 '19 at 22:06
  • Making expiration date makes sense. What line throws `NullPointerException` exactly ? – Ruslan Feb 01 '19 at 22:10
  • @Ruslan The one before the last, "memDateExpiryTxtFld.setText(reportDate);" I'm assuming it has something to do with the line before it as well where I tried to format into a String – LordSniffles Feb 01 '19 at 22:12
  • 1
    I'd suspect that `memDateExpiryTxtFld` is `null` before `reportDate` – MadProgrammer Feb 01 '19 at 22:18
  • just check where `memDateExpiryTxtFld` is instantiated and make sure it is not `null` – Ruslan Feb 01 '19 at 22:20
  • @Ruslan @MadProgrammer Thanks guys, I realised that the `memDateExpiryTxtFld` wasn't instantiated in the first place! Fixed the problem and I'm getting the proper results now. – LordSniffles Feb 02 '19 at 01:04
  • Don’t store the expiration as text, store it as a `LocalDate` in Java (and as an appropriate date type in your database if you store it there). Also don’t use `SimpleDateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead as mentioned use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 04 '19 at 09:08

1 Answers1

3

First of all java.util.Date is outdated, try to use java.time.* instead.

For adding one year to a certain date you can use:

import java.time.LocalDate;

LocalDate expirationDate = LocalDate.now().plusYears(1);

And formatting:

import java.time.format.DateTimeFormatter;

DateTimeFormatter formatter = new DateTimeFormatter.ofPattern("E yyyy/MM/dd");
String formattedDate = expirationDate.format(formatter)
Max Farsikov
  • 2,451
  • 19
  • 25
  • @Andreas, thanks :) – Max Farsikov Feb 01 '19 at 22:25
  • Hello Max, thanks for your input. Weirdly enough, I've tried using the DateTimeFormatter with the imported libraries, however the program still doesn't seem to see it, as it keeps saying 'Cannot find symbol - class DateTimeFormatter', I am using BlueJ if that is of any help. – LordSniffles Feb 02 '19 at 00:44
  • 1
    @LordSniffles probably you are missing an import. I have updated my answer, please check it. – Max Farsikov Feb 02 '19 at 11:33