运行时间(Java版本)—转换毫秒到时分秒日期
第一种方式:
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()));
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
运行时间(Java版本)—转换毫秒到时分秒日期的更多相关文章
- Java条件查询涉及到时分秒
关于Oralce数据库 的日期时间查询: 下面我们先来看一组日期数据 表:myDate 列:time; 1998-8-7 23:45:33.3 1998-8-7 11:22:21.5 1998-8-7 ...
- java System.currentTimeMillis()毫秒值和具体日期值互相转换
System.currentTimeMillis()与日期 间是可以相互转换的,通过 SimpleDateFormat dateformat = new SimpleDateFormat(" ...
- js中的时间转换—毫秒转换成日期时间
转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...
- Java编程的逻辑 (32) - 剖析日期和时间
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Java时间转换
package com.fh.util; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseE ...
- 你的程序支持复杂的时间调度嘛?如约而来的 java 版本
你的程序支持复杂的时间调度嘛? 这篇文章介绍了时间适配器的c#版本,是给客户端用的,服务器自然也要有一套对应的做法,java版本的 [年][月][日][星期][时间] [*][*][*][*][*] ...
- java Date获取 年月日时分秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Java知多少(77)日期和时间类
Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...
- Java常用类(I)-时间和日期
java开发中,常涉及到时间的处理,这里就做一个总结,同样也是一个笔记. 相关类及概念 1. java.util.Date:表示特定的瞬间,精确到毫秒.由于API 不易于实现国际化,日期和时间字段之间 ...
随机推荐
- MFC 将文件拖进对话框获得文件信息
非常多软件都支持直接将文件拖进去进行处理的功能,详细一点如暴风影音,将视频或者音频文件拖进去就会自己主动開始播放,那么这个功能在MFC上面怎么实现的呢?事实上非常easy,过程例如以下: 第一步:将对 ...
- C++输入输出进制、数据宽度与对齐、精度、取整
cout<<setw(4)<<setfill('0')<<a<<endl; ////样例输出 a=41输出 0041 1.数的进制 [转载]未完的c++ ...
- hdu 4454 Stealing a Cake(三分之二)
pid=4454" target="_blank" style="">题目链接:hdu 4454 Stealing a Cake 题目大意:给定 ...
- Android - 和其他APP交互 - 获得activity的返回值
启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...
- java中float/double浮点数的计算失精度问题(转)
如果我们编译运行下面这个程序会看到什么? public class Test { public static void main(String args[]) { ...
- 使用Canvas和Paint自己绘制折线图
主要用于Canvas一个特别简单的小demo. 能够手动点击看每一个月份的数据.很easy.就是用paint在canvas上画出来的. 主要内容就是计算左边价格的位置,以下日期的位置,三根虚线的位置, ...
- nginx: [emerg] the size 10485760 of shared memory zone "cache_one" conflicts with already declared size 0
注意配置段中的区域包含关系. proxy_cache_patch 要在proxy_cache前已经定义. what seems to be the problem? [emerg]: the size ...
- Windows Phone 8 ControlTiltEffect
/* Copyright (c) 2010 Microsoft Corporation. All rights reserved. Use of this sample source code is ...
- Unicode字段也有collation
原文:Unicode字段也有collation 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/11/unicode-collation.aspx ...
- spring mvc中实现csrf安全防御简记
1.csrf是什么 csrf全称是Cross-site request forgery,http://en.wikipedia.org/wiki/Csrf 危害:使受害用户在不经意间执行了不是用户意愿 ...