ITAndroid项目实战Android知识

Java与安卓中时间的处理

2016-08-01  本文已影响454人  关玮琳linSir

Java与安卓中时间的处理~

虽然我们叫做java与安卓中的时间处理,但是其实无论在什么语言中时间的处理都是相通的,下面我们就介绍一下时间处理的机制.

常用的时间格式大概分为以下三种:

        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");

这三种格式的时间我们可以很简单的构造出来,今天在公司的项目中遇到了这样一个问题,与其说遇到了问题,也可以说是我们采用这样的一个方法来解决我们的问题。在移动端,我们采集用户的出行信息,我们是按照“年-月-日”这个格式采集的,然后我们会发给服务器,服务器会根据我们的时间进行排序,不知道大家想排序的时候会有什么好的办法,我们后台采用的办法是,将所有的时间全部转换成秒,也就是说从1970年1月1日,到现在一共经历了多少秒,这个数字是一个长整型,只要这样就可以很轻松的进行排序,他也会将这个长整型返回给我们,我们只需要进行简单的处理就可以将这个秒转换年、月、日了,下面我就介绍三种方式将秒如何转换为年、月、日,当然相互转换是同理的,在这里就不给出了。


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));
    }
}

结果: 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));
    }
}

结果: 2016-08-01



public class Test2 {
    
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()));
    }
}

结果: 2016-08-01

上一篇下一篇

猜你喜欢

热点阅读