Solved

I'm looking to convert from 24hour clock to 12hour using a set driver

Posted on 2004-10-27
214 Views
Last Modified: 2010-03-31
I have a set driver.........

import java.io.*;

public class TimeTest {
  static BufferedReader keyboard = new
    BufferedReader (new InputStreamReader (System.in));
  static PrintWriter screen = new PrintWriter (System.out, true);



  public static void main (String[] args)
  {
    try
    {
    TimeCheck tc;
    int entry;

    screen.print ("Time on 24hr timetable? "); screen.flush();
    entry = Integer.parseInt(keyboard.readLine().trim());

    tc = new TimeCheck(entry);
    if (tc.isValid())
      screen.println ("Thats" + tc.getHours() + "." +
        tc.getMinutes() + tc.timeOfDay());
    else screen.println("Can't cope");
    }
    catch (Exception e)
    {
    screen.println ("Input Problem");
    }
  }
}




And need to create the main class to allow it to run.
I've been mucking about with it for ages but going more in circles than anything else!
Any suggestions...please!!

Thanks in advance
0
Question by:c18drm
    34 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Use two SimpleDateFormats
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Or simply subtract 12 hours from tc.getHours()  ;-)
    0
     

    Author Comment

    by:c18drm
    But the problem is that the input comes in the form of a normal time eg 1400, 2300.
    How can I get that change into hours/minutes.
    I can't simply subtract 12 from the input
    0
     
    LVL 16

    Assisted Solution

    by:imladris
    I'm not quite clear on what TimeCheck is or does.
    But "military time" (like 1400) could be split up into hours and minutes with division and mod:

    // convert it to integer:

    int mt=-1;
    try
    {   mt=Integer.parseInt(entry);
    } catch(NumberFormatException e)
    {   // input problem
    }
    int hours=mt/100;
    int min=mt%100;

    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Presumably you parse this input when constructing TimeCheck (unposted)?
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    ... such as

    public TimeCheck(String entry) {
          hours = Integer.parseInt(entry.substring(0, 3));
          minutes = Integer.parseInt(entry.substring(2));      
    }
    0
     

    Author Comment

    by:c18drm
    Not sure I understand the comment from imladris.

    As for comment from CEHJ,
    what will the     (entry.substring(0, 3)); do??

    don't get that bit
    0
     

    Author Comment

    by:c18drm
    also, would i need to parse this from a string to integer in my TimeCheck constructor if it has been parse'd at that stage in the driver?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>what will the     (entry.substring(0, 3)); do??

    Get the hours from the entry.

    TimeCheck may as well have a method to return 12hr format

    public int getHours12() {
        return hours > 12? (hours - 12) : hours;
    }
    0
     
    LVL 16

    Expert Comment

    by:imladris
    mt=Integer.parseInt(entry);

    This line converts the String of "1400" to a number 1400.
    Once you have a number you can do arithmetic operations with it. So:

    int hours=mt/100;

    This will divide mt (which is the number 1400) by 100, and leaving no remainder. So hours will wind up being 14. You could now, if needed, check if it is greater than 12, and then subtract 12, if that is desirable.

    int min=mt%100;

    This will get the remainder after dividing mt by 100. In this case min would wind up being 0. If mt were 1410, the min would wind up being 10.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>
    also, would i need to parse this from a string to integer in my TimeCheck constructor if it has been parse'd at that stage in the driver?
    >>

    No - i just did it that way for convenience
    0
     

    Author Comment

    by:c18drm
    >>what will the     (entry.substring(0, 3)); do??

    Get the hours from the entry.

    In that case what does the (0,3) stand for? don't see where they've come from.
    Thanks for all the help so far by the way
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    It means 'cut the entry String before the 3rd character, and save the first bit into the variable'
    0
     
    LVL 12

    Expert Comment

    by:Giant2
    See here for an explanation about Date and Time.
    http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html

    Bye, Giant.
    0
     

    Author Comment

    by:c18drm
    so for example if done in the some notation as my driver where the string is parsed to an integer in there its obviously not going to let me say substring(in my constructor) as there are no strings involved.

    this is the problem i'm havin as i have no flexibility with the given driver
    0
     
    LVL 12

    Expert Comment

    by:Giant2
    ...and use the String properties here:
    http://docs.rinet.ru/WebJPP/ch10.htm

    Hope these links could help you.
    Giant.
    0
     
    LVL 12

    Expert Comment

    by:Giant2
    ooops. the before link was about J++.
    See here for Java1.4
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    If it's easier to use int, then use imladris' approach
    0
     
    LVL 16

    Expert Comment

    by:imladris
    Right, entry is an integer (converted from the input).

    So you can manipulate it by breaking out hours and minutes (as I indicated). You could then recombine it and pass it in to TimeCheck.

    tc=new TimeCheck(hours*100+min);
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    If your TimeCheck class is in 24hr, then this will give you 12hr:

    int hours = tc.getHours();
    if (hours > 12) {
          hours -= 12;
    }
    0
     
    LVL 12

    Expert Comment

    by:Giant2
    Here for converting into number formats & other things
    http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/Strings/
    0
     
    LVL 86

    Accepted Solution

    by:
    This may be all you need depending on how TimeCheck is implemented:


    import java.io.*;

    public class TimeTest {
          static BufferedReader keyboard = new
                      BufferedReader(new InputStreamReader(System.in));
          static PrintWriter screen = new PrintWriter(System.out, true);


          public static void main(String[] args) {
                try {
                      TimeCheck tc;
                      int entry;

                      screen.print("Time on 24hr timetable? ");
                      screen.flush();
                      entry = Integer.parseInt(keyboard.readLine().trim());

                      tc = new TimeCheck(entry);
                      if (tc.isValid()) {
                            int hours = tc.getHours();
                            int minutes = tc.getMinutes();
                            screen.println("Thats" + hours + "." + minutes + tc.timeOfDay());
                            if (hours > 12) {
                                  int hours12 = hours - 12;
                                  screen.println("Thats" + hours12 + "." + minutes + tc.timeOfDay() + " in 12-hour clock");
                            }
                      }
                      else {
                            screen.println("Can't cope");
                      }
                }
                catch (Exception e) {
                      screen.println("Input Problem");
                }
          }
    }
    0
     

    Author Comment

    by:c18drm
    ok here we go......here is a copy of my TimeCheck class that i've prepared and been fiddling about with for a while.
    There may be bits in the wrong place etc.
    And by now you'll probably be getting the idea that i'm not too good at this.......yet!!!!!

    But hopefully I'll get there.
    Anyway when i run my program it compiles fine, asks me to enter the time then...nothing.
    Anyway here we go:


    import java.io.*;
    import java.text.DecimalFormat;

    class TimeCheck {

      private int hour;    //0-23
      private int minute;  //0-59
      private int time;

       public TimeCheck() {
      }


      public TimeCheck(int time){
        this.time=time;
      }




      public boolean isValid(){
        if(
        time == ((time>=0 && time<2400) ? time:0))
          return true;
        else return false;
        }

      public int getHours(){
        hour = time/100;
        if (hour>12){
          hour = hour - 12;
        }
        return hour;
      }

      public int getMinutes(){
        minute = time%100;
        return minute;
      }

      public String timeOfDay(){
        DecimalFormat twoDigits = new DecimalFormat("00");

        return((getHours() ==12 || getHours() == 0) ? 12: getHours() %12)+
        twoDigits.format( getMinutes())+  ":" +
        (getHours() <12 ? "am" : "pm");
        }

    }
    0
     
    LVL 16

    Expert Comment

    by:imladris
    Job 1 is to figure out where the program "went".

    If you have a debugger this is relatively easy. If not, you will have to make do with old fashioned method of putting in print statements to find out how far it got.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    It's working here ...
    0
     

    Author Comment

    by:c18drm
    CEHJ,
    How'd you mean its working??!!!
    U get it to do everything its supposed to?
    Been away working hence the delayed reply
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Well i'll show you what i get - hang on going to input the time here (2235)...
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    (Only tweaked on the println):



    C:\java\dumpit>java TimeTest
    Time on 24hr timetable? 2235
    That's 10.35 1035:am

    (ok the 'am's wrong but ..)
    0
     

    Author Comment

    by:c18drm
    oh you dancer!
    so i was at least on sort-of the right lines!!
    must be the way my JDE's set-up, i'll try it on the Linux machines we use at uni.
    Look, much appreciation to you guys who've helped.
    Very quick reply service aswell.
    How do the points on the site get rewarded? what do you guys need to get the credit for helping?
    0
     

    Author Comment

    by:c18drm
    ps, what do u mean by only tweaked on the println? nothing major i'm guessing.

    i try to run my program and I can half-see the result coming ok but the window very quickly shuts down. using JBuilder and it is set up using packages, not quite got to grips with the ins and outs of it yet!
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>ps, what do u mean by only tweaked on the println? nothing major i'm guessing.

    No - only a space and an apostrophe changed here and there ;-

    >>using JBuilder and it is set up using packages, not quite got to grips with the ins and outs of it yet!

    You must also get familiar with running at the command line too.

    >>How do the points on the site get rewarded?

    By you ;-) Just accept an answer. You can split points if you want
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0
     
    LVL 12

    Expert Comment

    by:Giant2
    Sorry for not help you.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Suggested Solutions

    Title # Comments Views Activity
    nested interface 5 22
    Logs to delete in Windows VCenter server ? 11 55
    factorial example challenge 10 34
    IT Company 5 38
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now