• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with DateFormat usage  RSS feed

 
Victor Puertas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The software I am working on get a date in a specific format:
- dd/MM/yyyy HH:mm

Therefore I used the class DateFormat:
1.- Created a object formatter:
DateFormat formatter = new SimpleDateFormat(dd/MM/yyyy HH:mm)
2.- I get the value from the sdin from the option from:
String from1 = (String) options.valueOf("from")
3.- Apply the format
Date from = (Date) formatter.parse(from1)

For the executions that does not have the option from in the stdin I receive the following error:
java.lang.NullPointerException
        at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1380)
        at java.text.DateFormat.parse(DateFormat.java:355)

I am new in Java.. Does anyone know what the problem is?
Thanks.

 
Knute Snortum
Sheriff
Posts: 4080
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that from1 is null.  You probably should check to see if from1 is null before trying to parse it.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a sidenote: On which version of Java are you developing?
Personally I prefer the newer Java 8 time API where you could use java.time.LocalDateTime combined with java.time.format.DateTimeFormatter

You then can first create a LocalDateTime object using or StdIn input, and then pass that object to the formatter.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great advice Arco.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!