Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Date-time comparison

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!