• 运行时间(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()));
    	}
    }

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

  • 相关阅读:
    Smith Numbers POJ
    HDU
    dp HDU
    POJ
    HDU
    LOOPS HDU
    水题,P1789 【Mc生存】插火把 (暴力即可)
    LOOPS
    Coprime (单色三角形+莫比乌斯反演(数论容斥))
    莫比乌斯函数 51nod-1240(合数分解试除法)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4621862.html
Copyright © 2020-2023  润新知