Java time display


Dear Experts,

I can't get one part of my homework to print.  The code works expect for this part.  I tried about 30 different ways to make this print, but I can't get.  The code for military time works, but not for regular time.  Again, I don't want anyone to do my homework, I just need some help.  My assignment is due today.


    for(int j =0; j<=100; j++) {  
         
     SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a");
     java.util.Date dd1 = sdf2.parse("5:30:25",new ParsePosition(0));      
       
     java.util.Date dd2 = new java.util.Date(dd1.getTime() + j*1000L);
          System.out.println("" + sdf2.format(dd2));
              
              }         
import javax.swing.*;
import javax.swing.text.DateFormatter;

import java.text.DateFormat;
import java.text.ParsePosition;// recommended by Eclipse
import java.text.SimpleDateFormat;
import java.util.Date;

public class Time2 {
	static // http://download.oracle.com/javase/6/docs/api/java/util/Date.html
	//http://download.oracle.com/javase/6/docs/api/java/util/Date.html#Date%28int,%20int,%20int%29
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      java.util.Date dd2; // from the above web site

	 public Time2(){
	    dd2=setTime(0, 0, 0);
	 }

     public Time2(int h, int m, int s){
	   dd2=setTime(h, m, s);
	 }

	public java.util.Date  setTime(int h, int m, int s) {
        String hour = "" + h;
        String minute = "" + m;
        String second = "" + s;

        while(hour.length()<2)hour = "0"+h;
        while(minute.length()<2)minute = "0"+m;
        while(second.length()<2)second = "0"+s;

        // format for the below line is public Date parse(String text, ParsePosition pos) from Eclipse help
         java.util.Date dd1 = sdf.parse(h+":" +m + ":" + s,new ParsePosition(0));

        return dd1;

	}

	public String getMilitaryTime()
	   {

           return sdf.format(dd2);

	   }

	 public String getRegularTime()
	   {

           SimpleDateFormat sdf101 = new SimpleDateFormat("hh:mm:ss a");
           return sdf101.format(dd2);
 
	   }

	 public void tick() {

           dd2 = new java.util.Date(dd2.getTime() + 1000); // add 1000 milliseconds or 1 

	}

	public static void main(String[] args) {

			Time2 t1 = new Time2(23, 59, 55);
			Time2 t2 = new Time2(15, 30, 25);
			t2.getRegularTime();

			String output = "";

		    output = "The 1st Military time is: "
		    + t1.getMilitaryTime() +
		    "\nThe 1st Regular time is:  " + t1.getRegularTime();
                   t1.tick();


		    output += "\n\nThe 2nd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 2nd Regular time is:  " +
		    t1.getRegularTime();
                         t1.tick();


   	        output += "\n\nThe 3rd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " +
		    t1.getRegularTime();

   	        t1.tick();
   	        output+= "\n\n 4th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();
   	        
   	        t1.tick();
   	         
   	        output+= "\n\n5th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();
   	        
   	        t1.tick();
    	    output+= "\n\n6th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();
   
   	        t1.tick();
    	    output+= "\n\n7th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();    	    
    	    
   	        t1.tick();
    	    output+= "\n\n8th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();    	    
    	    
   	        t1.tick();
    	    output+= "\n\n9th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();    
    	    
   	        t1.tick();
    	    output+= "\n\n10th  "+t1.getMilitaryTime() +"  "+ t1.getRegularTime();  
    	        
		    JOptionPane.showMessageDialog( null, output,
		    "Time for Assigment 4",
		    JOptionPane.INFORMATION_MESSAGE );
		    
		    
	       
	        for(int j =0; j<=100; j++) {
	        	
	        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
	        java.util.Date dd1 = sdf1.parse("15:30:25",new ParsePosition(0));	
	        
	        java.util.Date ddM = new java.util.Date(dd1.getTime() + j*1000L);
	        System.out.println("" + sdf1.format(ddM));
	        
	        }
	        
	  	    for(int j =0; j<=100; j++) {  
	  	    	
	        SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a");
	        java.util.Date dd1 = sdf2.parse("5:30:25",new ParsePosition(0));	
	        
	        java.util.Date dd2 = new java.util.Date(dd1.getTime() + j*1000L);
	        System.out.println("" + sdf2.format(dd2));
	        
	        }	     
	 System.exit( 0 );
	}
}

Open in new window

WalterAPOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

you need to has AM or PM accordint to format ("hh:mm:ss a")
                               for(int j =0; j<100; j++) {

     SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a");
     java.util.Date dd1 = sdf2.parse("05:30:25 AM",new ParsePosition(0));

     java.util.Date dd2 = new java.util.Date(dd1.getTime() + j*1000L);
          System.out.println("" + sdf2.format(dd2));

              }          

Open in new window

0
for_yanCommented:
                            for(int j =0; j<100; j++) {

     SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a");
     java.util.Date dd1 = sdf2.parse("05:30:25 AM",new ParsePosition(0));

     java.util.Date dd2 = new java.util.Date(dd1.getTime() + j*1000L);
          System.out.println("" + sdf2.format(dd2));

              }          

Open in new window


output:
05:30:25 AM
05:30:26 AM
05:30:27 AM
05:30:28 AM
05:30:29 AM
05:30:30 AM
05:30:31 AM
05:30:32 AM
05:30:33 AM
05:30:34 AM
05:30:35 AM
05:30:36 AM
05:30:37 AM
05:30:38 AM
05:30:39 AM
05:30:40 AM
05:30:41 AM
05:30:42 AM
05:30:43 AM
05:30:44 AM
05:30:45 AM
05:30:46 AM
05:30:47 AM
05:30:48 AM
05:30:49 AM
05:30:50 AM
05:30:51 AM
05:30:52 AM
05:30:53 AM
05:30:54 AM
05:30:55 AM
05:30:56 AM
05:30:57 AM
05:30:58 AM
05:30:59 AM
05:31:00 AM
05:31:01 AM
05:31:02 AM
05:31:03 AM
05:31:04 AM
05:31:05 AM
05:31:06 AM
05:31:07 AM
05:31:08 AM
05:31:09 AM
05:31:10 AM
05:31:11 AM
05:31:12 AM
05:31:13 AM
05:31:14 AM
05:31:15 AM
05:31:16 AM
05:31:17 AM
05:31:18 AM
05:31:19 AM
05:31:20 AM
05:31:21 AM
05:31:22 AM
05:31:23 AM
05:31:24 AM
05:31:25 AM
05:31:26 AM
05:31:27 AM
05:31:28 AM
05:31:29 AM
05:31:30 AM
05:31:31 AM
05:31:32 AM
05:31:33 AM
05:31:34 AM
05:31:35 AM
05:31:36 AM
05:31:37 AM
05:31:38 AM
05:31:39 AM
05:31:40 AM
05:31:41 AM
05:31:42 AM
05:31:43 AM
05:31:44 AM
05:31:45 AM
05:31:46 AM
05:31:47 AM
05:31:48 AM
05:31:49 AM
05:31:50 AM
05:31:51 AM
05:31:52 AM
05:31:53 AM
05:31:54 AM
05:31:55 AM
05:31:56 AM
05:31:57 AM
05:31:58 AM
05:31:59 AM
05:32:00 AM
05:32:01 AM
05:32:02 AM
05:32:03 AM
05:32:04 AM

Open in new window

0
for_yanCommented:

sdf2.parse("05:30:25",new ParsePosition(0)); causes erorr becaues feild  AM or PM is required accodring to format ("hh:mm:ss a");

as a result parse methoth return null
and coases null pointer exception on the next .line
where you want to invoke method dd.getTime() on null value

thsi will work
sdf2.parse("05:30:25 AM",new ParsePosition(0));

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WalterAPOAuthor Commented:
Genius Yan,

It works great.  Thank you .  A simple AM or PM and it cause an error.
0
CEHJCommented:
The proper way to increment dates in Java is with a Calendar btw. Avoid using your own arithmetic as it's unnecessary and error-prone
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.