Time Conversion Program in Java

Introduction


Time is the uncertain proceeded with advance of presence and occasions that happen in clearly irreversible progression from the past through the present to what's to come. Time is a part amount of different estimations used to succession occasions, to look at the term of occasions or the interims amongst them, and to evaluate rates of progress of amounts in material reality or in the cognizant experience. Time is frequently alluded to as a fourth measurement, alongside three spatial measurements. There are two time model which we are using up until now. they are 24-hour and 12-hour clock.

The 24-hour clock is the tradition of time keeping in which the day keeps running from midnight to midnight and is partitioned into 24 hours, showed by the hours go since midnight, from 0 to 23. This framework is the most ordinarily utilized time documentation on the planet today.

On the other hand, 12-hour clock is a time tradition in which the 24 hours of the day are partitioned into two periods: a.m. (from the Latin, ante meridiem, which means before early afternoon) and p.m. (post meridiem, which means past late morning). Every period comprises of 12 hours numbered: 12 (going about as zero), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11. The 24 hour/day cycle begins at 12 midnight (regularly demonstrated as 12 a.m.), goes through 12 twelve (frequently showed as 12 p.m.), and proceeds to the midnight by the day's end. The 12-hour clock was created after some time from the mid-second thousand years BC to the sixteenth century AD.

Now, we'd like to convert 12-hour format into 24-hour format. So, input must be followed this format : hh:mm:ss<AM/PM> (without < or >). as the input is 12-hour then hh must be from 1 to 12. Minute and second constraint must between 0 to 60. Remember , Midnight 12:00:00AM is on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.

Code


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Solution {


 static String timeConversion(String s) {
        // Complete this function
     String result ="",temp="",amOrPm="";
     int hour,minute,second;
     Solution checker = new Solution();
     
     String[] times = s.split(":");
     hour= Integer.parseInt(times[0]);
     minute= Integer.parseInt(times[1]);

     second= Integer.parseInt(checker.splitTime(times[2],1) ) ;
     amOrPm+=checker.splitTime(times[2], 2);
     
     //Process Hour PM
     if(amOrPm.equalsIgnoreCase("PM")){
      if(hour==12){
       hour-=12;
      }
      hour+=12;
     }else{
      if(hour==12){
       hour-=12;
      }
     }
     String hourStr= (String.valueOf(hour).length()==1)? "0"+hour : ""+hour ;
     String minuteStr = (String.valueOf(minute).length()==1)? "0"+minute : ""+minute ;;
     String secondStr = (String.valueOf(second).length()==1)? "0"+second : ""+second ;;
     result=result+hourStr+":"+minuteStr+":"+secondStr;

     
     return result;
    }
    
    String splitTime(String a,int mode){
     String num="", time="";
     Solution checker = new Solution();
     for (int i=0;i<a.length();i++){
      if (checker.isNum(a.charAt(i))){
       num+=a.charAt(i);
      }
      else{
       time+=a.charAt(i);
      }
     }
     if (mode == 1){
      return num;
     }else{
      return time;
     }
    }
    
    Boolean isNum(char c){
     for (int y=0;y<10;y++){
      if(c==String.valueOf(y).charAt(0)){
       return true;
      }
     }
     return false;
     
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = timeConversion(s);
        System.out.println(result);
    }
} 

Analysis

There  are four function in listing code above. Main function is the essential function in class if you want your code to work. Fuction isNum used for checking whether variable in parameter is number or not. Function spliTime used for split second and AM or PM. Function timeConversion used for processing convert 12-hour to 24-hour format.

Firstly, when the code run, the first line of code which are executed is main function. we make Scanner object to store input to string variable. Then call timeConversion(String inpu) fungstion with string variable as argument.

In function timConversion(), the given string split into three part which are hour, minute, and second (still combine with AM or PM). Second variable need split again to get wheather it is AM or PM. We used splitTime() function to split it. The idea is simple, just store number in num variable if character inside is number and vice versa if it is not a number. That's why we call isNUm() function to check it.

After all is done, then merge it into one string and print it in console.

Screenshoot





Comments

Popular posts from this blog

Quantization of Image Data Implementation in C#

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Computer Graphics Project "MINION" Source Code with Glut OpenGL