Convert
and Compare with Todays Date
Make String to java.util.Date
I am getting date from HTML form i.e.
String days=req.getParameter("days");
String months=req.getParameter("months");
String year=req.getParameter("year");
String job_start=days+"-"+months+"-"+year;
String job_end=days1+"-"+months1+"-"+year1;
How can I convert and compare with todays date?
My Code:
=========
String query5="select sysdate from dual";
PreparedStatement ps5=cnn.prepareStatement(query5);
rs=ps5.executeQuery();
if(rs.next()){
java.util.Date today_date=rs.getDate(1);
if(job_start.after(today_date)){
//option
}else{
//option
}
String query5="select sysdate from dual";
PreparedStatement ps5=cnn.prepareStatement(query5);
rs=ps5.executeQuery();
if(rs.next()){
java.util.Date today_date=rs.getDate(1);
if(job_start.after(today_date)){
//option
}else{
//option
}
What I did is; certainly its wrong aproach as:
1. I am trying to write String instead of java.util.Date
2. I am getting today date with oracle and don't know
as to how can in get from Java
Solution:
java.util.Date has many deprecated methods, instead use
java.util.GregorianCalendar.
Code:
import java.util.GregorianCalendar;
public class DateTest {
public static void main(String[] args)
{
int dayOfMonth
= 19;
int month
= 10; // month is zero based
int year =
2009;
// First date
GregorianCalendar
gc1 =
new GregorianCalendar(year, month, dayOfMonth);
// Second
date
GregorianCalendar
gc2 =
new GregorianCalendar(year, month, dayOfMonth);
System.out.println(gc1.compareTo(gc2));
}
}
import java.util.GregorianCalendar;
public class DateTest {
public static void main(String[] args) {
int dayOfMonth = 19;
int month = 10; // month is zero based
int year = 2009;
// First date
GregorianCalendar gc1 =
new GregorianCalendar(year, month, dayOfMonth);
// Second date
GregorianCalendar gc2 =
new GregorianCalendar(year, month, dayOfMonth);
System.out.println(gc1.compareTo(gc2));
}
}
Notice that if you have the dates represented in String,
you can compare them using the equals() method.
Or
Check out DateFormat and its most-often used subclass
SimpleDateFormat.
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|