我正在尝试将 System.currentTimeMillis 转换为 (hh:mm:ss) 的当前时间格式。到目前为止,这是我尝试过的,但效果不佳。
Long currentTime = System.currentTimeMillis();
int hours;
int minutes;
int seconds;
String getSecToStr = currentTime.toString();
String getTimeStr = getSecToStr.substring(8,13);
seconds = Integer.parseInt(getTimeStr);
minutes = seconds / 60;
seconds -= minutes * 60;
hours = minutes / 60;
minutes -= hours * 60;
String myResult = Integer.toString(hours) + ":" + Integer.toString(minutes) + ":" + Integer.toString(seconds);
System.out.println("Current Time Is: " + myResult);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?非常感激!
将毫秒格式化为SimpleDate格式时,我遇到了一个奇怪的结果:
输出是:
Start date time: 11/06/30 09:45:48:970
End date time: 11/06/30 09:45:52:831
Execution time: 01:00:03:861
Run Code Online (Sandbox Code Playgroud)
脚本:
long dateTimeStart = System.currentTimeMillis();
// some script execution here
long dateTimeEnd = System.currentTimeMillis();
"Start date time: " + GlobalUtilities.getDate(dateTimeStart, "yy/MM/dd hh:mm:ss:SSS");
"End date time: " + GlobalUtilities.getDate(dateTimeEnd, "yy/MM/dd hh:mm:ss:SSS");
"Execution time: " + GlobalUtilities.getDate((dateTimeEnd - dateTimeStart), "hh:mm:ss:SSS");
Run Code Online (Sandbox Code Playgroud)
方法:
public static String getDate(long milliseconds, String format)
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(milliseconds);
}
Run Code Online (Sandbox Code Playgroud)
知道为什么执行时间值如此偏离?它应该是00:00:03:861,而不是01:00:03:861
谢谢
我正在尝试将毫秒日期转换为years months weeks和的数量days.
例如:5 months, 2 weeks and 3 days或1 year and 1 day.
我不想要:7 days或者4 weeks>这应该是1 week和1 month.
我尝试了几种方法,但它总是变得类似7 days and 0 weeks.
我的代码:
int weeks = (int) Math.abs(timeInMillis / (24 * 60 * 60 * 1000 * 7));
int days = (int) timeInMillis / (24 * 60 * 60 * 1000)+1);
Run Code Online (Sandbox Code Playgroud)
我必须在天数上加1,因为如果我有23个小时应该是1天.
请解释如何正确转换它,我认为有更有效的方法来做到这一点.
真的很简单的问题,但我无法在任何地方找到简单的解释.我想以毫秒为单位获取当前时间:
val current = System.currentTimeMillis()
Run Code Online (Sandbox Code Playgroud)
并将结果转换为日期时间格式,如2017-02-11T15:16:38.437Z.如果问题不清楚,这适用于Scala.
我到处寻找将秒转换为hh:mm:ss但找不到合适的
我创建了一个程序,允许用户输入两个不同的时间,然后计算差异
输入的时间分为hh*3600 - mm*60 - ss然后转换为秒并相互减去以秒为单位计算差异
例如12:12:12和13:13:13会给我3661秒,但我不知道如何将差异转换回hh:mm:ss
任何帮助,将不胜感激
我想创建一个库,因为我找不到一个将秒或毫秒转换成时间.我的意思是:
1)如果我有61秒,时间格式为:1:01(不是1:1)
2)如果我相当于1小时1分钟,我希望它显示相同:1:01:00
我通过以下结构实现了这一目标:
public String secondsToTime(int seconds){
String format = "";
int currentMinutes = 0, currentHour = 0;
if((seconds / 60) > 0 ) {
currentMinutes = seconds / 60;
seconds = seconds - currentMinutes * 60;
}
if(currentMinutes >= 60)
{
currentHour = currentMinutes / 60;
currentMinutes = currentMinutes - currentHour * 60;
}
if(currentHour == 0) {
if(currentMinutes < 10 && seconds < 10)
format = "0"+currentMinutes+":0"+seconds;
else if(currentMinutes > 9 && seconds < 10)
format = …Run Code Online (Sandbox Code Playgroud) 我有一个特定任务所需的估计时间,以分钟为单位.我怎么能把它放在一个JFormattedTextField的格式HH:mm:ss?
我需要格式化我的时间字符串,例如:
int time = 160;
Run Code Online (Sandbox Code Playgroud)
这是我的示例代码:
public static String formatDuration(String minute) {
String formattedMinute = null;
SimpleDateFormat sdf = new SimpleDateFormat("mm");
try {
Date dt = sdf.parse(minute);
sdf = new SimpleDateFormat("HH mm");
formattedMinute = sdf.format(dt);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedMinute;
// int minutes = 120;
// int h = minutes / 60 + Integer.parseInt(minute);
// int m = minutes % 60 + Integer.parseInt(minute);
// return h + "hr " + m + "mins";
}
Run Code Online (Sandbox Code Playgroud)
我需要将它显示为2小时40分钟.但我不知道如何追加"hrs"和"mins".要求不使用任何库.
如果您过去做过类似的事情,请随时提供帮助.谢谢你!
我懂了.但我似乎无法弄清楚如何获得剩余天数/小时/分钟/秒.我在调试模式下运行它并生成:2776799998.以毫秒为单位的2776799998是2天,1小时15分钟.(当发布这个时).
什么是正确的方法?
Calendar cal = Calendar.getInstance();
cal.set(2012, 6, 28, 16, 0);
long endTime = cal.getTimeInMillis();
long currentTime = System.currentTimeMillis();
long remaining = endTime - currentTime;
long seconds = remaining / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
Run Code Online (Sandbox Code Playgroud) "输出应该是这样的(回输输入是个好主意):你输入了500,000秒,即5天,18小时,53分钟和20秒.(5天18:53:20小时)
所以你们可以告诉我应该怎么做,理解和做到最简单的方法是什么,请帮助大家,我真的卡住了,他说"没有硬编码",我不确定它是什么但是我认为他希望我们为它们分配常量.