Java與安卓中時(shí)間的處理

Java與安卓中時(shí)間的處理~

雖然我們叫做java與安卓中的時(shí)間處理,但是其實(shí)無(wú)論在什么語(yǔ)言中時(shí)間的處理都是相通的,下面我們就介紹一下時(shí)間處理的機(jī)制.

常用的時(shí)間格式大概分為以下三種:

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
        SimpleDateFormat format2 = new SimpleDateFormat("yy-MM-dd H:m:s");
        SimpleDateFormat format3 = new SimpleDateFormat("y-M-d H:m:s");

這三種格式的時(shí)間我們可以很簡(jiǎn)單的構(gòu)造出來(lái),今天在公司的項(xiàng)目中遇到了這樣一個(gè)問(wèn)題,與其說(shuō)遇到了問(wèn)題,也可以說(shuō)是我們采用這樣的一個(gè)方法來(lái)解決我們的問(wèn)題。在移動(dòng)端,我們采集用戶的出行信息,我們是按照“年-月-日”這個(gè)格式采集的,然后我們會(huì)發(fā)給服務(wù)器,服務(wù)器會(huì)根據(jù)我們的時(shí)間進(jìn)行排序,不知道大家想排序的時(shí)候會(huì)有什么好的辦法,我們后臺(tái)采用的辦法是,將所有的時(shí)間全部轉(zhuǎn)換成秒,也就是說(shuō)從1970年1月1日,到現(xiàn)在一共經(jīng)歷了多少秒,這個(gè)數(shù)字是一個(gè)長(zhǎng)整型,只要這樣就可以很輕松的進(jìn)行排序,他也會(huì)將這個(gè)長(zhǎng)整型返回給我們,我們只需要進(jìn)行簡(jiǎn)單的處理就可以將這個(gè)秒轉(zhuǎn)換年、月、日了,下面我就介紹三種方式將秒如何轉(zhuǎn)換為年、月、日,當(dāng)然相互轉(zhuǎn)換是同理的,在這里就不給出了。


  • 第一種方式
    采用java中Util包中的Date類
public class Test3 {
    public static void main(String[] args) {

        Long time = 1469980800000L;
        System.out.println(time);

        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(d));
    }
}

結(jié)果: 2016-08-01


  • 第二種方式
import java.util.Calendar;
import java.util.TimeZone;

public class Test {

    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(1469980800000L));
    }
}

結(jié)果: 2016-08-01


  • 第三種方式(和前兩種方式不一樣,完全沒(méi)有依賴java內(nèi)部自帶的工具類)

public class Test2 {
    
public String getYearMonthDayHourMinuteSecond(long timeMillis) {
        int timezone = 8; // 時(shí)區(qū)
        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);// 時(shí)
        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()));
    }
}

結(jié)果: 2016-08-01

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容