Java:如何将时间作为字符串输入并计算差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35250036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to input time as string and calculate difference
提问by Annabeth Chase
I want to make a java program where the user enters a start time and an end time. from there I want the program to calculate the difference between the times? This should be the output
我想制作一个用户输入开始时间和结束时间的 java 程序。从那里我希望程序计算时间之间的差异?这应该是输出
Enter first time: 12:56 pm
Enter second time: 1:03 pm
time difference is 7 minutes
All i just know is the formula to how to calculate and find the minutes.
我只知道如何计算和找到分钟的公式。
回答by Prateek
sorry I'm not quite clear with what you need. If you want to convert the time entered by the user to a time stamp and then calculate the difference, this would help:
抱歉,我不太清楚您需要什么。如果要将用户输入的时间转换为时间戳,然后计算差异,这将有所帮助:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Time {
public static void main(String args[]) throws ParseException{
Scanner input = new Scanner(System.in);
System.out.print("Enter first time (hh:mm aa): ");
String time = input.nextLine();
System.out.println();
System.out.print("Enter second time (hh:mm aa): ");
String time2 = input.nextLine();
System.out.println();
DateFormat sdf = new SimpleDateFormat("hh:mm aa");
Date d1 = sdf.parse(time);
Date d2 = sdf.parse(time2);
System.out.println("Time: " + sdf.format(d1));
System.out.println("Time: " + sdf.format(d2));
if(d1.after(d2)){
long diffMs = d1.getTime() - d2.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes and "+sec+" seconds.");
}
if(d1.before(d2)){
long diffMs = d2.getTime() - d1.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes and "+sec+" seconds.");
}
if(d1.equals(d2)){
System.out.println("The difference is 0 minutes and 0 seconds.");
}
}
}
The only limitation is that you have to limit your user to enter the time in the given format, allowing user to enter time in any format will make things bad for you to handle.
唯一的限制是你必须限制你的用户以给定的格式输入时间,允许用户以任何格式输入时间都会让你处理不好。
I hope it helps :)
我希望它有帮助:)
回答by swanand keskar
To accept time from user in HH:MM:SS format using regular expression
使用正则表达式以 HH:MM:SS 格式接受用户的时间
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetTime {
public static void main(String[] args) {
String ip="";
String pattern="^([01]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)$";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the time");
ip=sc.nextLine();
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(ip);
if(m.find())
{
System.out.println("Time"+m.group(0));
System.out.println("hh "+m.group(1));
System.out.println("mm "+m.group(2));
System.out.println("ss "+m.group(3));
}
else
{
System.out.println("NO MATCH! format mismatch");
}
}
}
回答by Reaz Murshed
String startTime = "08/16/2011 12:56:00";
String endTime = "08/16/2011 13:56:00";
// Customize your own date format
SimpleDateFormat format = new SimpleDateFormat("MM/DD/YYYY hh:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(startTime));
long startMillis = c.getTimeInMillis();
c.setTime(format.parse(endTime));
long endMillis = c.getTimeInMillis();
System.out.println("time difference is " + ((endMillis - startMillis)/1000) + " seconds");