编写一个java程序用以将AM/PM格式的时间转换为24小时格式,求大神完成代码
输入:9:30pm/输出:21:30输入:9:30am/输出:9:30输入:12:30pm/输出:12:30输入:9:30/输出:9:30输入:19:30/输出:19:3...
输入: 9:30pm / 输出: 21:30
输入: 9:30am / 输出: 9:30
输入: 12:30pm / 输出: 12:30
输入: 9:30 / 输出: 9:30
输入: 19:30 / 输出: 19:30
public classTimeConverter {
public static voidmain(String[] args) {
TimeConverter cont = newTimeConverter();
// Test
System.out.println("9:30pm -> "+ cont.convert("9:30pm"));
System.out.println("9:30am -> "+ cont.convert("9:30am"));
System.out.println("12:30pm -> "+ cont.convert("12:30pm"));
System.out.println("9:30 -> "+ cont.convert("9:30"));
System.out.println("19:30 -> "+ cont.convert("19:30"));
}
publicString convert(String ampm) {
//在此处写出解决问题的程序代码 …
}
} 展开
输入: 9:30am / 输出: 9:30
输入: 12:30pm / 输出: 12:30
输入: 9:30 / 输出: 9:30
输入: 19:30 / 输出: 19:30
public classTimeConverter {
public static voidmain(String[] args) {
TimeConverter cont = newTimeConverter();
// Test
System.out.println("9:30pm -> "+ cont.convert("9:30pm"));
System.out.println("9:30am -> "+ cont.convert("9:30am"));
System.out.println("12:30pm -> "+ cont.convert("12:30pm"));
System.out.println("9:30 -> "+ cont.convert("9:30"));
System.out.println("19:30 -> "+ cont.convert("19:30"));
}
publicString convert(String ampm) {
//在此处写出解决问题的程序代码 …
}
} 展开
4个回答
展开全部
SimpleDateFormat objSDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strCurrentTime = objSDateFormat.format(Date类型的时间);
注:大写的HH为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”
这个更全
实现思路就是输入一个时间,之后会输出相应的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 {
}
String strCurrentTime = objSDateFormat.format(Date类型的时间);
注:大写的HH为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”
这个更全
实现思路就是输入一个时间,之后会输出相应的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 {
}
展开全部
public String convert(String ampm) {
// 在此处写出解决问题的程序代码 …
if(ampm.contains("am")){
return ampm.substring(0, ampm.indexOf("am"));
}else if(ampm.contains("pm")){
int i = ampm.indexOf(":");
String s= ampm.substring(0, i);
int hour = Integer.parseInt(s);
if(hour >= 12){
String res = ampm.substring(0, ampm.indexOf("pm"));
return res;
}else{
String sHour = hour+12 + "";
return sHour + ampm.substring(ampm.indexOf(":"), ampm.indexOf("pm"));
}
}else{
return ampm;
}
}
完美解决 看在我这么辛苦的情况下请给采纳加精
// 在此处写出解决问题的程序代码 …
if(ampm.contains("am")){
return ampm.substring(0, ampm.indexOf("am"));
}else if(ampm.contains("pm")){
int i = ampm.indexOf(":");
String s= ampm.substring(0, i);
int hour = Integer.parseInt(s);
if(hour >= 12){
String res = ampm.substring(0, ampm.indexOf("pm"));
return res;
}else{
String sHour = hour+12 + "";
return sHour + ampm.substring(ampm.indexOf(":"), ampm.indexOf("pm"));
}
}else{
return ampm;
}
}
完美解决 看在我这么辛苦的情况下请给采纳加精
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public String convert(String ampm){
if(ampm.contains("am")){
ampm=ampm.substring(0,ampm.length()-2);
}else if(ampm.contains("pm")){
ampm=ampm.substring(0,ampm.length()-2);
int hour=Integer.parseInt(ampm.split(":")[0]);
if(hour<12){
ampm=(hour+12)+":"+ampm.split(":")[1];
}
}
return ampm;
}
你看看对不对,写的不太好,请见谅
追问
我想自己输入时间,能改下代码吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class AmPmUtil
{
public static void main(String[] args)
{
try
{
System.out.println(AmPmUtil.convert("9:30pm"));
System.out.println(AmPmUtil.convert("9:30am"));
System.out.println(AmPmUtil.convert("12:30pm"));
System.out.println(AmPmUtil.convert("9:30"));
System.out.println(AmPmUtil.convert("19:30"));
}
catch (final ParseException e)
{
e.printStackTrace();
}
}
/**
* 转换到24小时
* @param ampm
* @return
* @throws ParseException
*/
private static String convert(String ampm) throws ParseException
{
final SimpleDateFormat format = new SimpleDateFormat("HH:mm");
SimpleDateFormat parse = null;
if (ampm.indexOf("m") > -1)
{
parse = new SimpleDateFormat("hh:mma", Locale.ENGLISH);
}
else
{
parse = format;
}
return format.format(parse.parse(ampm));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询