如何确定 SimpleDateFormat 使用的是秒还是分钟
How to find out if SimpleDateFormat is using seconds or minutes
我正在制作一款带有计时器的游戏,它会告诉你最后完成游戏需要多长时间,这是一款基本游戏,所以有些人可能会在不到一分钟的时间内通关,所以我想知道有什么方法可以检测分钟是否等于 00 那么我可以将文本设置为说 00:12 秒,如果它不等于 00 那么它会说 01:12 分钟。这是我用来计算时间的代码。
Date start = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
而最后一次的计算就是
Date now = new Date();
sdf.format(new Date(now.getTime() - start.getTime()))
long start = System.currentTimeMillis();
/*
* Your code
*/
long diff = System.currentTimeMillis() - start;
long seconds = (diff / 1000) % 60;
long minutes = (diff / (1000 * 60)) % 60;
long hours = (diff / (1000 * 60 * 60)) % 24;
System.out.println("Total " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
请参阅以下链接了解其他可能的解决方案,
Java-How to calculate accurate time difference while using Joda Time Jar
How can I calculate a time span in Java and format the output?
已修复。
Date start = new Date();
SimpleDateFormat sdfm = new SimpleDateFormat("mm");
SimpleDateFormat sdfs = new SimpleDateFormat("ss");
Date now = new Date();
if(sdfm.format(new Date(now.getTime() - start.getTime())).equals("00")){
JOptionPane.showMessageDialog(null, "You Won!! It only took you " + sdfs.format(new Date(now.getTime() - start.getTime())) + " Seconds", "You Won!!", 1);
}else{
JOptionPane.showMessageDialog(null, "You Won!! It only took you " + sdfm.format(new Date(now.getTime() - start.getTime())) + ":" + sdfs.format(new Date(now.getTime() - start.getTime())) + " Minutes", "You Won!!", 1);
System.exit(0);
我正在制作一款带有计时器的游戏,它会告诉你最后完成游戏需要多长时间,这是一款基本游戏,所以有些人可能会在不到一分钟的时间内通关,所以我想知道有什么方法可以检测分钟是否等于 00 那么我可以将文本设置为说 00:12 秒,如果它不等于 00 那么它会说 01:12 分钟。这是我用来计算时间的代码。
Date start = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
而最后一次的计算就是
Date now = new Date();
sdf.format(new Date(now.getTime() - start.getTime()))
long start = System.currentTimeMillis();
/*
* Your code
*/
long diff = System.currentTimeMillis() - start;
long seconds = (diff / 1000) % 60;
long minutes = (diff / (1000 * 60)) % 60;
long hours = (diff / (1000 * 60 * 60)) % 24;
System.out.println("Total " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
请参阅以下链接了解其他可能的解决方案,
Java-How to calculate accurate time difference while using Joda Time Jar
How can I calculate a time span in Java and format the output?
已修复。
Date start = new Date();
SimpleDateFormat sdfm = new SimpleDateFormat("mm");
SimpleDateFormat sdfs = new SimpleDateFormat("ss");
Date now = new Date();
if(sdfm.format(new Date(now.getTime() - start.getTime())).equals("00")){
JOptionPane.showMessageDialog(null, "You Won!! It only took you " + sdfs.format(new Date(now.getTime() - start.getTime())) + " Seconds", "You Won!!", 1);
}else{
JOptionPane.showMessageDialog(null, "You Won!! It only took you " + sdfm.format(new Date(now.getTime() - start.getTime())) + ":" + sdfs.format(new Date(now.getTime() - start.getTime())) + " Minutes", "You Won!!", 1);
System.exit(0);