需要一个java程序,12小时和24小时制的互相转换
可以显示以下3个选择1-24小时转换12小时制2-12小时制转换24小时制3-退出例如,输入1,就是选择24小时转12小时输入1330,得出结果01:30这样可以自动选择...
可以显示以下3个选择
1-24小时转换12小时制
2-12小时制转换24小时制
3-退出
例如,输入1,就是选择24小时转12小时
输入1330,得出结果 01:30
这样可以自动选择转换的类型
输入选择:
java程序输出类型这样子的
11:30 AM = 11:30
11:30 PM= 23:30
23:30= 11:30PM 展开
1-24小时转换12小时制
2-12小时制转换24小时制
3-退出
例如,输入1,就是选择24小时转12小时
输入1330,得出结果 01:30
这样可以自动选择转换的类型
输入选择:
java程序输出类型这样子的
11:30 AM = 11:30
11:30 PM= 23:30
23:30= 11:30PM 展开
2个回答
展开全部
实现思路就是输入一个时间,之后会输出相应的12小时和24小时效果展示:
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
while (true) {
System.out.println("Enter time in 24-hour notation:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
try {
outTime(line);
} catch (TimeFormatException e) {
System.out.println("There is no such time as " + line);
System.out.println("Try again:");
continue;
}
sc = new Scanner(System.in);
line = sc.nextLine();
if ("n".equalsIgnoreCase(line)) {
break;
}
}
System.out.println("End of program");
}
public static void outTime(String line) throws TimeFormatException {
SimpleDateFormat _24time = new SimpleDateFormat("HH:mm");
SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",
Locale.ENGLISH);
try {
String[] array = line.split(":");
if (Integer.parseInt(array[0]) < 0
|| Integer.parseInt(array[0]) > 23) {
throw new TimeFormatException();
}
if (Integer.parseInt(array[1]) < 0
|| Integer.parseInt(array[1]) > 59) {
throw new TimeFormatException();
}
System.out.println(_12time.format(_24time.parse(line)));
System.out.println("Again?(y/n)");
} catch (Exception e) {
throw new TimeFormatException();
}
}
}
class TimeFormatException extends Exception {
}
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
while (true) {
System.out.println("Enter time in 24-hour notation:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
try {
outTime(line);
} catch (TimeFormatException e) {
System.out.println("There is no such time as " + line);
System.out.println("Try again:");
continue;
}
sc = new Scanner(System.in);
line = sc.nextLine();
if ("n".equalsIgnoreCase(line)) {
break;
}
}
System.out.println("End of program");
}
public static void outTime(String line) throws TimeFormatException {
SimpleDateFormat _24time = new SimpleDateFormat("HH:mm");
SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",
Locale.ENGLISH);
try {
String[] array = line.split(":");
if (Integer.parseInt(array[0]) < 0
|| Integer.parseInt(array[0]) > 23) {
throw new TimeFormatException();
}
if (Integer.parseInt(array[1]) < 0
|| Integer.parseInt(array[1]) > 59) {
throw new TimeFormatException();
}
System.out.println(_12time.format(_24time.parse(line)));
System.out.println("Again?(y/n)");
} catch (Exception e) {
throw new TimeFormatException();
}
}
}
class TimeFormatException extends Exception {
}
展开全部
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class $ {
public static void main(String... _) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("请输入时间(如:1330):");
String time = in.next();
Format f = new SimpleDateFormat("HHmm");
while (true) {
System.out.println("1.24小时;2,12小时;3:退出");
int idx = in.nextInt();
if (idx == 1) {
Date d = (Date) f.parseObject(time);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE));
}
if (idx == 2) {
Date d = (Date) f.parseObject(time);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE));
}
if (idx == 3) {
System.exit(0);
}
}
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class $ {
public static void main(String... _) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("请输入时间(如:1330):");
String time = in.next();
Format f = new SimpleDateFormat("HHmm");
while (true) {
System.out.println("1.24小时;2,12小时;3:退出");
int idx = in.nextInt();
if (idx == 1) {
Date d = (Date) f.parseObject(time);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE));
}
if (idx == 2) {
Date d = (Date) f.parseObject(time);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE));
}
if (idx == 3) {
System.exit(0);
}
}
}
}
追问
能否使用这种类型
追答
也是可以的,但是时间格式的问题,不建议用"字符串截取"和"long"类型来处理,那样会比较麻烦
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询