java 时分秒转毫秒_运行时间(Java版本)—转换毫秒到时分秒日期

本文介绍了两种在Java中将时间毫秒数转换为年月日时分秒的方法。第一种方法使用`Calendar`类,第二种方法通过手动计算。这两种方法都考虑了时区和闰年的调整,可以将当前时间或任意毫秒数转换为格式化的日期时间字符串。
摘要由CSDN通过智能技术生成

第一种方式:

import java.util.Calendar;

import java.util.TimeZone;

public class Test {

/**

* 将毫秒转换为年月日时分秒

*

* @author GaoHuanjie

*/

public String getYearMonthDayHourMinuteSecond(long timeMillis) {

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));

calendar.setTimeInMillis(timeMillis);

int year=calendar.get(Calendar.YEAR);

int month=calendar.get(Calendar.MONTH) + 1;

String mToMonth=null;

if (String.valueOf(month).length()==1) {

mToMonth="0"+month;

} else {

mToMonth=String.valueOf(month);

}

int day=calendar.get(Calendar.DAY_OF_MONTH);

String dToDay=null;

if (String.valueOf(day).length()==1) {

dToDay="0"+day;

} else {

dToDay=String.valueOf(day);

}

int hour=calendar.get(Calendar.HOUR_OF_DAY);

String hToHour=null;

if (String.valueOf(hour).length()==1) {

hToHour="0"+hour;

} else {

hToHour=String.valueOf(hour);

}

int minute=calendar.get(Calendar.MINUTE);

String mToMinute=null;

if (String.valueOf(minute).length()==1) {

mToMinute="0"+minute;

} else {

mToMinute=String.valueOf(minute);

}

int second=calendar.get(Calendar.SECOND);

String sToSecond=null;

if (String.valueOf(second).length()==1) {

sToSecond="0"+second;

} else {

sToSecond=String.valueOf(second);

}

return year+ "-" +mToMonth+ "-" +dToDay+ " "+hToHour+ ":" +mToMinute+ ":" +sToSecond;

}

public static void main(String[] args) {

System.out.println(new Test().getYearMonthDayHourMinuteSecond(System.currentTimeMillis()));

}

}

另外一种方式:

public class Test {

/**

* 将毫秒转换为年月日时分秒

*

* @author GaoHuanjie

*/

public String getYearMonthDayHourMinuteSecond(long timeMillis) {

int timezone = 8; // 时区

long totalSeconds = timeMillis / 1000;

totalSeconds += 60 * 60 * timezone;

int second = (int) (totalSeconds % 60);// 秒

long totalMinutes = totalSeconds / 60;

int minute = (int) (totalMinutes % 60);// 分

long totalHours = totalMinutes / 60;

int hour = (int) (totalHours % 24);// 时

int totalDays = (int) (totalHours / 24);

int _year = 1970;

int year = _year + totalDays / 366;

int month = 1;

int day = 1;

int diffDays;

boolean leapYear;

while (true) {

int diff = (year - _year) * 365;

diff += (year - 1) / 4 - (_year - 1) / 4;

diff -= ((year - 1) / 100 - (_year - 1) / 100);

diff += (year - 1) / 400 - (_year - 1) / 400;

diffDays = totalDays - diff;

leapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);

if (!leapYear && diffDays < 365 || leapYear && diffDays < 366) {

break;

} else {

year++;

}

}

int[] monthDays;

if (diffDays >= 59 && leapYear) {

monthDays = new int[] { -1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };

} else {

monthDays = new int[] { -1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

}

for (int i = monthDays.length - 1; i >= 1; i--) {

if (diffDays >= monthDays[i]) {

month = i;

day = diffDays - monthDays[i] + 1;

break;

}

}

return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

}

public static void main(String[] args) {

System.out.println(new Test().getYearMonthDayHourMinuteSecond(System.currentTimeMillis()));

}

}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值