• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How work with Date, DateFormat

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic