第一种方式:

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版本)—转换毫秒到时分秒日期的更多相关文章

  1. Java条件查询涉及到时分秒

    关于Oralce数据库 的日期时间查询: 下面我们先来看一组日期数据 表:myDate 列:time; 1998-8-7 23:45:33.3 1998-8-7 11:22:21.5 1998-8-7 ...

  2. java System.currentTimeMillis()毫秒值和具体日期值互相转换

    System.currentTimeMillis()与日期 间是可以相互转换的,通过 SimpleDateFormat dateformat = new SimpleDateFormat(" ...

  3. js中的时间转换—毫秒转换成日期时间

    转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...

  4. Java编程的逻辑 (32) - 剖析日期和时间

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  5. Java时间转换

    package com.fh.util; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseE ...

  6. 你的程序支持复杂的时间调度嘛?如约而来的 java 版本

    你的程序支持复杂的时间调度嘛? 这篇文章介绍了时间适配器的c#版本,是给客户端用的,服务器自然也要有一套对应的做法,java版本的 [年][月][日][星期][时间] [*][*][*][*][*] ...

  7. 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 ...

  8. Java知多少(77)日期和时间类

    Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...

  9. Java常用类(I)-时间和日期

    java开发中,常涉及到时间的处理,这里就做一个总结,同样也是一个笔记. 相关类及概念 1. java.util.Date:表示特定的瞬间,精确到毫秒.由于API 不易于实现国际化,日期和时间字段之间 ...

随机推荐

  1. C#(SuperWebSocket)与websocket通信

    原文:C#(SuperWebSocket)与websocket通信 客户端代码 点击可以查看一些关于websocket的介绍 <!DOCTYPE html> <html> &l ...

  2. Gradle构建多模块项目(转)

    废话不多说,直接进入主题. 1. 创建项目 首先创建项目,名称为 test: mkdir test && cd test gradle init 这时候的项目结构如下: ➜ test ...

  3. PHPSingleton模式的例子

    在这篇文章中PHPSingleton模式的解释不一定好!仅举它的一个例子.其目的是为了让自己通过一个例子来加深对Singleton模式的理解!这里,以供参考! 单例:能够简单的理解是通过一个类,仅仅能 ...

  4. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  5. The area面积计算

    Problem Description Ignatius bought a land last week, but he didn't know the area of the land becaus ...

  6. JSP 获得Spring 注射对象

    <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%&g ...

  7. springMVC项目异步处理请求的错误Async support must be enabled on a servlet and for all filters involved in async

    从github上down下来一个项目,springMVC-chat.作者全是用的注解,也就是零配置.这可苦了我,经过千辛万苦,终于集成到如今的项目中有一点样子了,结果报出来以下的错误.红色部分.解决方 ...

  8. Java设计模式(三)-修饰模式

    我们都知道.能够使用两种方式给一个类或者对象加入行为. 一是使用继承.继承是给一个类加入行为的比較有效的途径.通过使用继承,能够使得子类在拥有自身方法的同一时候,还能够拥有父类的方法.可是使用继承是静 ...

  9. 基本数据类型TypeScript

    TypeScript 前言 最近项目很急,所以没有什么时间回答关于Xamarin.Android方面的问题,也有一段时间没有更新.主要是手头很缺人,如果有谁有兴趣加入我们的话,可以私聊我,这样我就能继 ...

  10. 中文乱码?不,是 HTML 实体编码!(转)

    在 如何用 Nodejs 分析一个简单页面 一文中,我们爬取了博客园首页的 20 篇文章标题,输出部分拼接了一个字符串: var $ = cheerio.load(sres.text); var an ...