d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Need A Method To Convert 24 Hour Format To 12.
Add Reply New Topic New Poll
Member
Posts: 25,572
Joined: Jan 10 2009
Gold: 5,347.00
Feb 23 2017 12:16pm
Java. Really beginner stuff but I can't figure this out. I can do 12 to 24, but I'm having a hard time doing it the other way..

I just need a method that takes in the 24 hour format and converts it to the 12 hour format. I also need it to throw a TimeFormatException if anything other than 0000-2359 is entered.. Through a lot of research I was able to do this:

Code
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Time
{
private static Scanner sc;

public static void convertTime()
{
sc = new Scanner(System.in);

System.out.print("Enter the time in 24 hour time format (ex. 2153): ");
String currenttime = sc.nextLine();

try
{
Date d = new SimpleDateFormat("HHmm").parse(currenttime);
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
System.out.println(format.format(d));
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
convertTime();
}
}


But I didn't know how to write the exception in that? I have also never learned anything like that in my classes, so I didn't really want to use that code.

I also think I'd prefer to have something that doesn't use SimpleDateFormat/Date and all of that.. just for the sake of turning this in since he just wants a method.



If you can help me please PM me. Or if you want to make FG I guess I can just get more FG and pay you if you want to write the code. I need this by 5 PM EST.

This post was edited by Greeley on Feb 23 2017 12:22pm
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Feb 23 2017 03:35pm

The letters you use here: Date d = new SimpleDateFormat("HHmm").parse(currenttime);
Means something, depending if its in lower / upper case.
If you wanna do the opposite you need to write the same code using: hhmm


Take a look at this format:

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 23 2017 06:20pm
Edit:: Disregard this post I didn't read the bottom of your post after the code snippet. What I said has nothing to aid you.

Are you wanting to do this via java specific date-time methods, or do you have to do this yourself?

I would write it something along these lines:

Code
hours, minutes = "13:41".split(":")
raise TimeFormatException if hours.to_i > 24 || hours.to_i < 0
raise TimeFormatException if minutes.to_i > 59 || minutes.to_i < 0

if hours.to_i > 12
suffix = "pm"
hours = (hours.to_i - 12).to_s
else
suffix = "am"
end

puts "#{hours}:#{minutes} #{suffix}"
#1:41 pm


Be aware that this assumes the input time is in the correct format and does not include garbage data. More error handling is needed.

I would just use the date-time methods available in Java, why reinvent the wheel.

Edit:: My ruby is rusty and type conversions should of been handled during the split. Oh well.

This post was edited by AbDuCt on Feb 23 2017 06:24pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Feb 24 2017 02:31am
Code
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
private static Scanner sc;

public static void convertTime() throws TimeFormatException {
sc = new Scanner(System.in);

System.out.print("Enter the time in 24 hour time format (ex. 2153): ");
String currenttime = sc.nextLine();

try {
Date d = new SimpleDateFormat("HHmm").parse(currenttime);

if (d.before(new SimpleDateFormat("HHmm").parse("2400"))) {
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
System.out.println(format.format(d));
} else throw new TimeFormatException(currenttime);

} catch (ParseException e) {
e.printStackTrace();
}

}

public static void main(String[] args) {
try {
convertTime();
} catch (TimeFormatException e) {
e.printStackTrace();
}
}

private static class TimeFormatException extends Exception {
public TimeFormatException(String message) {
super("there is no such time as "+message);
}
}
}


Code
com.intellij.rt.execution.application.AppMain Main
Enter the time in 24 hour time format (ex. 2153): 2500
Main$TimeFormatException: there is no such time as 2500
at Main.convertTime(Main.java:21)
at Main.main(Main.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 0
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll