• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Converting a String (120135) to a Date object (with respect to time) and compare this time to current time

Hi guys if I had a String say "120134" and I wanted to
create a date object out of it (time) in the form of (hhmmss)
and I wanted to compare this time to the current time ... if it is the exact time or after the time (time i converted is what I am checking against) do something

Conceptually this looks right, but how do I do without having the date (just the times) itself in the object?




import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.*;

public class Test
{
public static void main(String args[])
{
   try
   {
      SimpleDateFormat f = new SimpleDateFormat("hhmmss");
     Date date = f.parse("120134");
     System.out.println("Check Time: " + date );
     
     GregorianCalendar c = new GregorianCalendar();
     Date d = c.getTime();
     System.out.println("Current Time: " + d);
     int i = d.compareTo(date);
     System.out.println("i =" + i);
     if ((i==0)|(i<0))
                //times are the same or the time has passed important time
                  {
                     //..do something
                }

                {
                     // where I willhave app sleep with a thread.sleep(100);
                }
     }
    catch (ParseException e)
   {
   System.out.println("exception thrown" + e);
   }
     }
}

Partly becuase it gives me

Check Time: Thu Jan 01 00:01:34 EST 1970
Current Time: Fri May 19 12:24:14 EDT 2006
i =1

thanks
ryno71


0
ryno71
Asked:
ryno71
1 Solution
 
CEHJCommented:
You could use two Calendar: set the hours minutes and second of the one to the other:

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
cal1.set(Calendar.HOUR, cal2.get(Calendar.HOUR));
etc.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
with out date how will you compare? you don't know which year also?

Actally its taking the default year as no date has been specified in the simple date format.
0
 
hoomanvCommented:
> with out date how will you compare ?

you can compare the hour/minute/second individualy
not the whole date
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ryno71Author Commented:
thanks guys for the input
0
 
ryno71Author Commented:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.*;

public class Test
{
public static void main(String args[])
{
   try
   {
      SimpleDateFormat f = new SimpleDateFormat("hhmmss");
     Date date = f.parse("120134");
     System.out.println("New Date: " + date );
     //Date currentday=new Date();
      
      
     
      
       Calendar cal1 = Calendar.getInstance();
       System.out.println("currently cal1: " + cal1.getTime());
       Calendar cal2 = Calendar.getInstance();
       System.out.println("currently cal2: " + cal2.getTime());
       cal1.setTime(date);
       int month = cal2.get(Calendar.MONTH);
     int day = cal2.get(Calendar.DAY_OF_MONTH);
     int year = cal2.get(Calendar.YEAR);
       System.out.println("currently Time: " + month + " " + day + "  " + year);
      
       cal1.set(year,month,day);
      
      
       System.out.println("tweaked current Time: " + cal1.getTime());
      
      
      
      
      
       System.out.println("set Time: " + cal1.getTime());
       System.out.println("current year: " + cal1.YEAR);
       System.out.println("current month: " + cal1.MONTH);
       System.out.println("current day: " + cal1.DAY_OF_MONTH);
      
       System.out.println("current Time: " + cal2.getTime());
       System.out.println("current year: " + cal2.YEAR);
       System.out.println("current month: " + cal2.MONTH);
       System.out.println("current day: " + cal2.DAY_OF_MONTH);
      
      
      
     
     
     boolean i = cal2.equals(cal1);
     System.out.println("i =" + i);
     if ((cal2.equals(cal1))|(cal2.after(cal1)))
                //times are the same or the time has passed important time
                  {
                     System.out.println("in here");
                }
      else
                {
                     System.out.println("in else");

                }
     }
    catch (ParseException e)
   {
   System.out.println("exception thrown" + e);
   }
     }
}
0
 
CEHJCommented:
:-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now