Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > Java

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 11-28-2004, 12:06 AM
Winkster Winkster is offline
Registered User
 
Join Date: Nov 2004
Posts: 2
Military Time Converter

I have written a class to convert military time to standard time. What I need to do is modify this code so a user can input the military time. I realize I can take out some extraneous code. Thanks.

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
// Time abstract data type (ADT) definition

public class Time
{
private int hour; // 0 - 23 (attributes)
private int minute; // 0 - 59 (attributes)
//private int second; // 0 - 59 (attributes)

private Date dt;
private long offset;

int number;

// Time constructor initializes each data member to zero.
// Ensures all Time objects start in a consistent state.
public Time( )
{
setOffset();
setTime(0,0); //removed seconds
}

// set a new Time value hour, minute
public void
setTime(int h, int m)throws IllegalArgumentException
{
if(h > 23 || h <0 || m > 59 ||m < 0 )
throw(new IllegalArgumentException());
hour = h;
minute = m;

dt = new Date(((h*3600)+(m*60))*1000-offset);
}

void setOffset()
{
Calendar c = Calendar.getInstance();
offset =c.get(c.ZONE_OFFSET);
}

// Print time in standard format
public void printStandard( )
{

System.out.println(new SimpleDateFormat("h:mm a").format(dt));
}

public void printMilitary()
{
System.out.println(new SimpleDateFormat("HHmm").format(dt));
}

// Driver to test simple class Time
public static void main( String args[ ] )
{



Time t = new Time( ); // instantiate object t of class time
System.out.print("\nThe initial standard time is ");
t.printStandard( );
System.out.print("\nThe initial military time is ");
t.printMilitary();

try
{
System.out.println("Enter time in 24 hour notation:");
t=SavitchIn.readLine();//THIS IS WHAT I NEED TO FIX???
t.setTime(13,47);
}
catch(IllegalArgumentException e){System.out.println(e);}

System.out.print("That is the same as ");
t.printStandard( );
System.out.print("That is the same as ");
t.printMilitary( );

try{t.setTime(99, 99);} // attempt invalid settings
catch(IllegalArgumentException e){System.out.println(e);}
t.printStandard( );


}
}
Reply With Quote
  #2  
Old 11-28-2004, 05:12 AM
sjalle's Avatar
sjalle sjalle is offline
Moderator
 
Join Date: Nov 2004
Location: Norway
Posts: 1,560
Put this line of code the main method:

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

Then implement this method for getting user input:

/**
* Gets user input and returns hours and minutes as an int[2]
* @param input the console
* @param type type of timestring requested ("military" or "standard");
* @return
*/
public static int [] getTimeInput(BufferedReader input, String type) {
String s1=null, s2=null;
try {
String ins=input.readLine().trim();
if (type.equals("military")) {
s1=ins.substring(0,2);
s2=ins.substring(2,4);
} else if (type.equals("standard")){
int ix=ins.indexOf(":");
s1=ins.substring(0,ix);
s2=ins.substring(ix+1);
}
return new int [] {Integer.parseInt(s1), Integer.parseInt(s2)};
} catch (NumberFormatException nfe) {
System.out.println("That was not an integer");
return null;
} catch (IOException ioe) {
System.out.println("You can't read this cause your computer just died");
return null; // ehem....
}
}

then use this in your main method:

System.out.println("Enter time in 24 hour notation:");
int [] sT=getTimeInput(input,"standard");
t.setTime(sT[0],sT[1]);
t.printStandard( );
t.printMilitary( );
System.out.println("Enter time in military notation:");
int [] mT=getTimeInput(input,"military");
t.setTime(mT[0],mT[1]);
t.printStandard( );
t.printMilitary( );
__________________
eschew obfuscation
Reply With Quote
  #3  
Old 11-28-2004, 03:56 PM
Winkster Winkster is offline
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks. I am incorporating this now and testing to see if it works the way I want.
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:21 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.