java 将int转换为时间_java实现时间格式转换(int整数类型的秒/毫秒---时分秒毫秒)...

1. 直接上干货:(可提取为工具类收藏哦 ^_^)

package com.drew.utils;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* long类型数字转换成时分秒毫秒格式

*

* @author zero 2019/04/11

*/

public class ConvertorTime {

public static void main(String[] args) {

int seconds = 17854, msec = 360000;// 秒,毫秒

System.out.println(secToTime(seconds));

System.out.println(msec + "毫秒转换格式时间:\t" + msecToTime(msec));

System.out.println("当前时间(时:分:秒.毫秒)\t" + new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));

}

/**

* 秒转换小时-分-秒analytics/util/DateUtil.java

*

* @param seconds 秒为单位 比如..600秒

* @return 比如...2小时3分钟52秒

*/

public static String secToTime(int seconds) {

int hour = seconds / 3600;

int minute = (seconds - hour * 3600) / 60;

int second = (seconds - hour * 3600 - minute * 60);

StringBuffer sb = new StringBuffer();

if (hour > 0) {

sb.append(hour + "小时");

}

if (minute > 0) {

sb.append(minute + "分");

}

if (second > 0) {

sb.append(second + "秒");

}

if (second == 0) {

sb.append("<1秒");

}

return sb.toString();

}

/**

* 将int类型数字转换成时分秒毫秒的格式数据

*

* @param time long类型的数据

* @return HH:mm:ss.SSS

* @author zero 2019/04/11

*/

public static String msecToTime(int time) {

String timeStr = null;

int hour = 0;

int minute = 0;

int second = 0;

int millisecond = 0;

if (time <= 0)

return "00:00:00.000";

else {

second = time / 1000;

minute = second / 60;

millisecond = time % 1000;

if (second < 60) {

timeStr = "00:00:" + unitFormat(second) + "." + unitFormat2(millisecond);

} else if (minute < 60) {

second = second % 60;

timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);

} else {// 数字>=3600 000的时候

hour = minute / 60;

minute = minute % 60;

second = second - hour * 3600 - minute * 60;

timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second) + "."

+ unitFormat2(millisecond);

}

}

return timeStr;

}

public static String unitFormat(int i) {// 时分秒的格式转换

String retStr = null;

if (i >= 0 && i < 10)

retStr = "0" + Integer.toString(i);

else

retStr = "" + i;

return retStr;

}

public static String unitFormat2(int i) {// 毫秒的格式转换

String retStr = null;

if (i >= 0 && i < 10)

retStr = "00" + Integer.toString(i);

else if (i >= 10 && i < 100) {

retStr = "0" + Integer.toString(i);

} else

retStr = "" + i;

return retStr;

}

}

2. 测试结果:

dd44582eaa35a0dcfb0576f96d942b7b.png

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言实现将一个毫秒转换成年月日分的程序: ```c #include <stdio.h> void msToDateTime(long long milliseconds) { // 每个月的天数(非闰年) int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 获取当前时间戳的数 long long seconds = milliseconds / 1000; // 计算年份 int year = 1970; while (1) { int daysInYear = 365; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { daysInYear = 366; // 当年是闰年 } if (seconds < daysInYear * 24 * 60 * 60) { break; } seconds -= daysInYear * 24 * 60 * 60; year++; } // 计算月份和日期 int month = 1; while (1) { int daysInMonth = daysInMonth[month - 1]; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { daysInMonth = 29; // 当月是闰年的二月 } if (seconds < daysInMonth * 24 * 60 * 60) { break; } seconds -= daysInMonth * 24 * 60 * 60; month++; } int day = seconds / (24 * 60 * 60) + 1; seconds = seconds % (24 * 60 * 60); // 计算小时、分钟int hour = seconds / (60 * 60); seconds = seconds % (60 * 60); int minute = seconds / 60; int second = seconds % 60; // 输出结果 printf("时间为:%d年%d月%d日 %02d时%02d分%02d\n", year, month, day, hour, minute, second); } int main() { long long milliseconds = 1689230512000; // 输入要转换毫秒数 msToDateTime(milliseconds); return 0; } ``` 运行以上程序,将毫秒数1689230512000转换成年月日时分秒时间为:2023年10月26日 00时01分52

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值