File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Date-time comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date-time comparison" Watch "Date-time comparison" New topic
Author

Date-time comparison

Nazneen Shaikh
Greenhorn

Joined: May 01, 2003
Posts: 21
Hi,
I am trying to do a date time comparison using the GregorianCalendar class. I have the following code where in I am trying to check the given date with the current date but it dosent seem to work.
it always returns false please help.
public boolean chkdates(String schDt)
{
//schDt obtained in format dd-mm-yyyy-hh24-mm-ss
int year=0;
int month=0;
int day=0;
int hours=0;
int mins=0;
System.out.println("in chk dates");

GregorianCalendar GCalCurr = new GregorianCalendar(year,month,day,hours,mins);
GregorianCalendar GCalSchd = new GregorianCalendar(year,month,day,hours,mins);

StringTokenizer st1 = new StringTokenizer(schDt,"-",false);

int schedday=Integer.parseInt(st1.nextToken());
GCalSchd.set(GregorianCalendar.DATE,schedday);

int schedmon=Integer.parseInt(st1.nextToken());
GCalSchd.set(GregorianCalendar.MONTH,schedmon);

int schedyear=Integer.parseInt(st1.nextToken());
GCalSchd.set(GregorianCalendar.YEAR,schedyear);

int schedhh=Integer.parseInt(st1.nextToken());
GCalSchd.set(GregorianCalendar.HOUR_OF_DAY,schedhh);

int schedmm=Integer.parseInt(st1.nextToken());
GCalSchd.set(GregorianCalendar.MINUTE,schedmm);

int schedss=Integer.parseInt(st1.nextToken());

if(GCalCurr.after(GCalSchd))
return true;
else
return false;
}
Thanks in advance
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Couple of things.
1). Unless you really need to, I don't recommend working directly with the GregorianCalendar class. Using the Calendar class should work just fine (even better, since it will support Localization). It also saves typing
2). To get the current date, just say

By saying

you are creating a Date that (to my machine) is Wednesday, 31 December of the year AD 2 -- roughly the time of Caesar Agustus
3). When you set the months, remember that months are zero-indexed. (This is the thing I hate the most about the Calendar class.) Setting your month to 3 sets the month to April, and setting the month to 12 sets the month to January and rolls the year up by one.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

you can simplify your code and make it more readable by using the java.text.SimpleDateFormat class:

[ May 02, 2003: Message edited by: Jamie Robertson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date-time comparison
 
Similar Threads
Problem with calculating difference of 2 date in days
How to get current date and time
parsing date as a string
Months and years
Date Format Problem And Hashtable ......