我需要将标准时间转换为传统时间。
答案 0 :(得分:4)
使用SimpleDateFormat的解决方案
private static String convertTimes(String stdTime) throws ParseException{
SimpleDateFormat stdF = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat tF = new SimpleDateFormat("hh:mm a");
return tF.format(stdF.parse(stdTime));
}
答案 1 :(得分:1)
查看DateFormat
,更具体地说SimpleDateFormat
。您可以使用它们来解析字符串中的日期(包括时间)并将它们转换回另一种格式。
有很多例子如何使用它,for example on how to convert date strings。您应该能够轻松地将它们用于您的用例。
答案 2 :(得分:1)
JB Nizet手动敲击头部 - 这是解释他的想法的代码。
public static void printTime(String time){
String[] time_split = time.split(":");
int hour = Integer.parseInt(time_split[0]);
int minute = Integer.parseInt(time_split[1]);
String am_pm;
if(hour>=12)
am_pm = "PM";
else
am_pm = "AM";
if(hour > 12)
hour -= 12;
if(hour == 0)
hour = 12;
System.out.printf("%d:%d%s\n",hour,minute,am_pm);
}
输出:
上午12时35分
答案 3 :(得分:1)
非常感谢任何帮助
让您的生活更轻松,并使用一个好的日期时间库来完成工作。避免捆绑的java.util.Date/Calendar类。使用:
以下是Joda-Time 2.3中的示例代码。
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
String timeString_24Hour = "14:35:22";
// From String to LocalTime object
DateTimeFormatter formatter_24HourTime = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = formatter_24HourTime.parseLocalTime( timeString_24Hour );
// From LocalTime object to String
DateTimeFormatter formatter_12HourTime = DateTimeFormat.forPattern("h:mm:ss aa");
String timeString_12Hour = formatter_12HourTime.print( localTime );
转储到控制台...
System.out.println( "timeString_24Hour: " + timeString_24Hour );
System.out.println( "localTime: " + localTime.toString() );
System.out.println( "timeString_12Hour: " + timeString_12Hour );
跑步时......
timeString_24Hour: 14:35:22
localTime: 14:35:22.000
timeString_12Hour: 2:35:22 PM
答案 4 :(得分:1)
我更改convertToTraditional()方法以使您的代码正常工作。
static String amPM;// will hold am/pm for standard time
static String traditionalTime;// will store traditional time from user
static int mins1, mins2, hours;// will store hours and minutes
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user
// input
int tryAgain = 1;// will initial do-while loop
System.out.println("Standard Time to Traditional Time Converter");
System.out.println("===========================================");
do {
System.out.println();
System.out.println("Input a time in Standard Form (HH:MM:SS):");
String standardTime = br.readLine();// user inputs time in standard
// form
System.out.println();
do {
if ((standardTime.length()) != 8) {
System.out.println("Invalid time entered.");
System.out
.println("Input a time in Standard Form that has this form HH:MM:SS ...");
standardTime = br.readLine();// user inputs time in standard
// form
System.out.println();
}
} while ((standardTime.length()) != 8);
convertToTraditional(standardTime);
// if (hours >= 12) {
// System.out.println(standardTime + " is equivalent to "
// + traditionalTime + " PM");
// }
// if (hours < 12) {
// System.out.println(standardTime + " is equivalent to "
// + traditionalTime + " AM");
// }
System.out.println(standardTime + " is equivalent to "
+ traditionalTime);
System.out.println();
System.out.println("Enter 1 to try again.");
tryAgain = Integer.parseInt(br.readLine());// user decides to try
// again
} while (tryAgain == 1);// will repeat if user enters 1
}// closes main body
public static void convertToTraditional(String standardTime) {
String strHours = standardTime.substring(0, 2); // "13"
String strMin = standardTime.substring(3, 5);
hours=Integer.parseInt(strHours);
if(hours>12)
{
hours=hours-12;
traditionalTime=Integer.toString(hours)+":"+strMin+" PM";
} else
{
traditionalTime=strHours+":"+strMin+" AM";
}
}