Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: problem with time manipulation!

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default problem with time manipulation!

    i need to add certain minutes to util.time variable, and to do that code implemented is
     public java.sql.Time getSQLTime(String time,int min) throws ParseException{
            DateFormat sdf = new SimpleDateFormat("hh:mm");
            Date date = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, (min+30));
    //System.out.println(" Minutes to be added:"+min);
     
            date = utilDateToSqlTime(calendar.getTime());
    //System.out.println("updated sql time"+date);
            return new java.sql.Time(date.getTime());
        }

    i parameter time is in (hh:mm) format and is converted into util.time object with help of parse method.
    this above method returns sql.time. this code works fine except when the is set to [12:min] say time= 12:30 and i have to add 150 minutes to it then expected outcome should be 15:00:00 but the actual out come is 03:00:00.
    note: when 11:30 is passed and minutes to add 150, then outcome is 14:00:00.

    this problem persist. check the output for the range 11:58 to 13:00 and minutes to be added is 150
     
    time passed:11:58	returned time	14:28:00
    time passed:11:59	returned time	14:29:00
    time passed:12:00	returned time	02:30:00
    time passed:12:01	returned time	02:31:00
    time passed:12:02	returned time	02:32:00
    time passed:12:03	returned time	02:33:00
    time passed:12:04	returned time	02:34:00
    time passed:12:05	returned time	02:35:00
    time passed:12:06	returned time	02:36:00
    time passed:12:07	returned time	02:37:00
    time passed:12:08	returned time	02:38:00
    time passed:12:09	returned time	02:39:00
    time passed:12:10	returned time	02:40:00
    time passed:12:11	returned time	02:41:00
    time passed:12:12	returned time	02:42:00
    time passed:12:13	returned time	02:43:00
    time passed:12:14	returned time	02:44:00
    time passed:12:15	returned time	02:45:00
    time passed:12:16	returned time	02:46:00
    time passed:12:17	returned time	02:47:00
    time passed:12:18	returned time	02:48:00
    time passed:12:19	returned time	02:49:00
    time passed:12:20	returned time	02:50:00
    time passed:12:21	returned time	02:51:00
    time passed:12:22	returned time	02:52:00
    time passed:12:23	returned time	02:53:00
    time passed:12:24	returned time	02:54:00
    time passed:12:25	returned time	02:55:00
    time passed:12:26	returned time	02:56:00
    time passed:12:27	returned time	02:57:00
    time passed:12:28	returned time	02:58:00
    time passed:12:29	returned time	02:59:00
    time passed:12:30	returned time	03:00:00
    time passed:12:31	returned time	03:01:00
    time passed:12:32	returned time	03:02:00
    time passed:12:33	returned time	03:03:00
    time passed:12:34	returned time	03:04:00
    time passed:12:35	returned time	03:05:00
    time passed:12:36	returned time	03:06:00
    time passed:12:37	returned time	03:07:00
    time passed:12:38	returned time	03:08:00
    time passed:12:39	returned time	03:09:00
    time passed:12:40	returned time	03:10:00
    time passed:12:41	returned time	03:11:00
    time passed:12:42	returned time	03:12:00
    time passed:12:43	returned time	03:13:00
    time passed:12:44	returned time	03:14:00
    time passed:12:45	returned time	03:15:00
    time passed:12:46	returned time	03:16:00
    time passed:12:47	returned time	03:17:00
    time passed:12:48	returned time	03:18:00
    time passed:12:49	returned time	03:19:00
    time passed:12:50	returned time	03:20:00
    time passed:12:51	returned time	03:21:00
    time passed:12:52	returned time	03:22:00
    time passed:12:53	returned time	03:23:00
    time passed:12:54	returned time	03:24:00
    time passed:12:55	returned time	03:25:00
    time passed:12:56	returned time	03:26:00
    time passed:12:57	returned time	03:27:00
    time passed:12:58	returned time	03:28:00
    time passed:12:59	returned time	03:29:00
    time passed:13:00	returned time	15:30:00
    thank you!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with time manipulation!

    I recommend you create an SSCCE that we can play with- how are you calling this function? How are you printing out those times?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: problem with time manipulation!

    Quote Originally Posted by KevinWorkman View Post
    I recommend you create an SSCCE that we can play with- how are you calling this function? How are you printing out those times?
    i'm not going to print these times, these are genrated so that every one who view my post come know about the problem.
    well, you are right i should make it small.
    i want to store start and end time of the show(movie), both of these times values are provided by the user (site admin) in order to create an Object of "show", and then use this object as a parameter to another method(insert_show()) of class showDAO to create a record in database.
    every thing is working fine except the above error.
    String start is decided according to show time selected, like if show is morning then show_start time will be 09:30 to duration of the movie +other mantaince timings like cleaning, interval etc.
    this problem arises when show =afternoon, and show_start=12:30 ( between noon to 1PM).
    any way code to produce those timing is
    public class show {
     
        private Time start;
        private Time end;
        private int dur;
        private Date moviedate;
        private String show;
        private String moviename;
        private String theater;
     
        public show() {
        }
     
        ;
        public show(Time s, Time e, String moviename, int dur, Date movie, String show, String theater) {
            this.start = s;
            this.end = e;
            this.dur = dur;
            this.moviedate = movie;
            this.show = show;
            this.moviename = moviename;
            this.theater = theater;
        }
     public java.sql.Time getSQLTime(String time, int min) throws ParseException {
            DateFormat sdf = new SimpleDateFormat("hh:mm");
            Date date = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, (min + 30));
    //System.out.println(" Minutes to be added:"+min);
     
            date = utilDateToSqlTime(calendar.getTime());
    //System.out.println("updated sql time"+date);
            return new java.sql.Time(date.getTime());
        }
     public static void main(String[] arg) throws ParseException {
            show sh = new show();
             String[] time={"11:58","11:59","12:00","12:01","12:02","12:03","12:04","12:05","12:06","12:07","12:08","12:09","12:10","12:11","12:12","12:13","12:14","12:15","12:16","12:17","12:18","12:19","12:20","12:21","12:22","12:23","12:24","12:25","12:26","12:27","12:28","12:29","12:30","12:31","12:32","12:33","12:34","12:35","12:36","12:37","12:38","12:39","12:40","12:41","12:42","12:43","12:44","12:45","12:46","12:47","12:48","12:49","12:50","12:51","12:52","12:53","12:54","12:55","12:56","12:57","12:58","12:59","13:00"};
                for (String s: time){
               //mm=Integer.toString(i);
             //  System.out.print("minutes"+mm);
     
             Time t = sh.getSQLTime(s, 120);
     
                System.out.println("time passed:"+s+"\treturned time\t" + t);
            }
        }
    }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: problem with time manipulation!

    Have you got the case of the 'h' right in your SimpleDateFormat?

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    arvindbis (September 30th, 2011)

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Solved: problem with time manipulation!

    Quote Originally Posted by Sean4u View Post
    Have you got the case of the 'h' right in your SimpleDateFormat?
    yeah that h should be H, and issue is resolved. i'm a noob.
    thank you guys!

Similar Threads

  1. String Manipulation.
    By Nemus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2011, 03:02 PM
  2. java run time problem?
    By kcs403 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 15th, 2011, 09:06 AM
  3. [SOLVED] Experiencing a run time error, don't know what the problem is
    By scooty199 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 10:21 AM
  4. question on list manipulation
    By andrefaelnar in forum Java Theory & Questions
    Replies: 11
    Last Post: August 18th, 2010, 07:30 PM
  5. String manipulation help
    By Duff in forum Loops & Control Statements
    Replies: 7
    Last Post: March 19th, 2010, 04:02 AM