aspose file tools
The moose likes Beginning Java and the fly likes How work with Date, DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How work with Date, DateFormat" Watch "How work with Date, DateFormat" New topic
Author

How work with Date, DateFormat

Al Pearson
Greenhorn

Joined: Dec 19, 2001
Posts: 18
I want to compare the date of an incoming cookie with today's date.
I can create today's ok with
Date datToday = new Date();
But how do I create an object with the date of the incoming Cookie. I would like to be able to just say something like:
Date datCookie = new Date("Sat Dec 15 19:53:44 CET 2001");
...but this is not legal. The Sun documentation says:
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
I find the documentation for DateFormat and other seemingly related classes (Calendar) to be confusing, with no sample code to point me in the right direction.
I'd appreciate some practical clues or sample code.
Alan

Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Here is a snippet that might help:

The String passed into the SimpleDateFormat can be whatever format you want (dd-MMM-YYYY dd-MM-YY dd-MM-YYYY). The setLenient() method allows you to determine if an exact date format match is needed.
Al Pearson
Greenhorn

Joined: Dec 19, 2001
Posts: 18
Sorry for the late reply, Paul - I's been like vacatin'.
Thanks for the clue. To solve my problem, how to compare the date string of an incoming cookie,
in format "dd/MM/yyyy", with today's date, I did this and it works fine.

Calendar calendar = new GregorianCalendar();
calendar.getInstance();
int intMonth = calendar.get(Calendar.MONTH);
String strToday = calendar.get(Calendar.DAY_OF_MONTH) +"/" +
++intMonth +"/" +
calendar.get(Calendar.YEAR);
SimpleDateFormat inFormatter = new SimpleDateFormat("dd/MM/yyyy");
Date datLastVisit = inFormatter.parse(strLastVisit, new ParsePosition(0));
Date datToday = inFormatter.parse(strToday, new ParsePosition(0));
if ( datLastVisit.before(datToday) ) {
etc, etc

a round of saspirellas on me!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How work with Date, DateFormat
 
Similar Threads
resolving java.lang.NumberFormatException
set cooki servlet and take form javascript
get the Yesterday's date ??
Date formatting
getting time