Operations on time (Java edition) -- converts milliseconds to years, months, days, hours, minutes, And seconds

Source: Internet
Author: User

Method 1:

Import java. util. calendar; import java. util. timeZone; public class Test {/*** converts millisecond to year, month, day, hour, minute, second ** @ author GaoHuanjie */public String getYearMonthDayHourMinuteSecond (long timeMillis) {Calendar calendar AR = Calendar ar. 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 ()));}}

Method 2:

Public class Test {/*** converts millisecond to year, month, day, hour, minute, second ** @ author GaoHuanjie */public String getYearMonthDayHourMinuteSecond (long timeMillis) {int timezone = 8; // Time Zone long totalSeconds = timeMillis/1000; totalSeconds + = 60*60 * timezone; int second = (int) (totalSeconds % 60 ); // seconds long totalMinutes = totalSeconds/60; int minute = (int) (totalMinutes % 60); // minute long totalHours = totalMinutes/60; int hour = (int )( TotalHours % 24); // time 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 ()));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.