关于时间的操作(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版)——将毫秒转换为年月日时分秒,布布扣,bubuko.com

时间: 2024-08-05 07:07:56

关于时间的操作(Java版)——将毫秒转换为年月日时分秒的相关文章

time.c 的Java实现(从timestamp计算年月日时分秒等数值)

time.c的Java实现 public class GMT { public static final int EPOCH_YEAR = 1970; public static final int[][] MONTH_DAYS = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; public static final long MSEC

Java计算年月日时分秒时间差(两个时间相减)

//测试主方法  public static void main(String[] args) {          Date currentTime = df.parse("2004-03-26 13:31:40");   //当前系统时间             Date firstTime = df.parse("2004-01-02 11:30:24");     //查询的数据时间          String str=getTime(currentTi

将毫秒转化成时分秒

//将毫秒转换成时分秒 public string formatLongToTimeStr(long a) { try { int hour = 0; int minute = 0; int second = 0; second = Convert.ToInt32(a) / 1000; if (second > 60) { minute = second / 60; second = second % 60; } if (minute > 60) { hour = minute / 60; m

jquery 的日期时间控件(年月日时分秒)

<!-- import package --> <script type="text/javascript" src="JS/jquery.js"></script> <script type="text/javascript" src="JS/jquery-ui-1.7.3/ui/jquery-ui-1.7.3.custom.js"></script> <sc

C#实现毫秒转换成时分秒的方法

本文实例讲述了C#实现毫秒转换成时分秒的方法.分享给大家供大家参考.具体实现方法如下: public static String formatLongToTimeStr(Long l) { String str = ""; int hour = 0; int minute = 0; int second = 0; second = l.intValue() / 1000; if (second > 60) { minute = second / 60; second = seco

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 37 38 39 40 41 42 43 package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class Test {     public vo

数据库存入年月日时分秒类型时间问题

获取当前时间以规定格式输出: Date date = new Date(); DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=df.format(date); 将自定义date存入数据库问题(精确到时分秒的情况): 1.实体类中声明Date类型变量(java.util.date); 2.action中声明String类型变量(因为一般datetimePicker传过来的是String); 3.

java 获取当前时间及年月日时分秒

java代码如下: package test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) { Calendar now = Calendar.getInstance(); System.out.println("年:" + now.get(Ca

Java版 中缀表达式转换为后缀表达式并求结果

基础知识 平时我们所说的一个算术表达式,例如:9+(3-1)*3+10/2即为中缀表达式,然而计算机无法计算中缀表达式的值,这是因为计算机无法进行带有左右括号和运算符的优先级这种混合运算.后缀表达式(又称 逆波兰式)的使用解决了上述问题. 上述的算术表达式的后缀表达式为:9 3 1 - 3 * + 10 2 / + 算法思想 如何计算上述后缀表达式的结果呢? 答案:从左到右扫描上述后缀表达式,然后: 1. 遇到数字入栈 2. 遇到运算符 将栈顶的前两个元素出栈,然后计算结果,并将计算的结果入栈