• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating a Date  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1780
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have two issues with the code where I need to split a string into two parts and calculate a date(given date plus interval)

First, value sent = "2y", issue is unit returns blank.

Second, date sent = "12/19/2012". I get an error on DateTime dtOrg = new DateTime(request.getParameter("mfgdate"));
java.lang.IllegalArgumentException: Invalid format: "12/19/2012" is malformed at "/19/2012"

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
First, value sent = "2y", issue is unit returns blank.


Splitting the string "2y" with the delimiter regex pattern of "[0-9]" will return a resultant array of size two. The first result is the blank string before the "2" delimiter, and the second is the "y" string after the "2" delimiter. So, when you dereference the array to fetch the first result, at index zero, you get a blank string.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
Second, date sent = "12/19/2012". I get an error on DateTime dtOrg = new DateTime(request.getParameter("mfgdate"));
java.lang.IllegalArgumentException: Invalid format: "12/19/2012" is malformed at "/19/2012"


It would help if you tell us where/what this DateTime class is from?

Henry
 
Steve Dyke
Ranch Hand
Posts: 1780
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
It would help if you tell us where/what this DateTime class is from?

Henry


It is the Joda library.

I have tried this also but still fails.

Error: java.text.ParseException: Unparseable date: "12/19/2012"

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
I have tried this also but still fails.

Error: java.text.ParseException: Unparseable date: "12/19/2012"


The format you submitted for SimpleDateFormat is not your format. The format of "yyyy m d" means a four digit year, followed by one digit minutes, followed by one digit day of the month, and each separated by spaces. Obviously, "12/19/2012" doesn't match this format.

Henry
 
Steve Dyke
Ranch Hand
Posts: 1780
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

The format you submitted for SimpleDateFormat is not your format. The format of "yyyy m d" means a four digit year, followed by one digit minutes, followed by one digit day of the month, and each separated by spaces. Obviously, "12/19/2012" doesn't match this format.

Henry


Okay I changed to this:

But this is what I get: 0019-07-02T00:28:24.000-05:50:36

I need a date two years after my provided date.

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of the mfgdate parameter (print it out to debug)?

Because it still looks like your parsing format does not match the actual format you are receiving.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something very peculiar about returning your shelf life as a String. How is that paint class designed?
 
Steve Dyke
Ranch Hand
Posts: 1780
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is something very peculiar about returning your shelf life as a String. How is that paint class designed?


I changed my code to use the Calendar class and it is working now.

Thanks for all the help.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that Java8 is here, with decent date‑time APIs, use LocalDate or similar rather than Calendar. Search for "Java Tutorials Date Time" and you should find lots of useful information.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:

FYI, the "m" in that pattern means minute, not month. Letters in date patterns are case sensitive, and for months you should use M.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!