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

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

0
WalterAPO
Asked:
WalterAPO
  • 3
1 Solution
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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