您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

android 开发中常用的日期转换 获取年月日时分秒 星期等等

时间:02-13来源:作者:点击数:

安卓开发中常用的时间戳与日期转换工具类

时间戳转年月日时分秒

获取单独的年 月 日 时 分 秒 星期 

距离某个日期的时间 精确到秒

public class DateUtils {
    
    private static final String TAG="DateUtils";

    public interface IOnTimeSelectListener{
        void onTimeSelectListener(Date date,View v);
    }

    private static TimePickerView pvCustomTime;

    /**
     * 日期字符串转10位时间戳
     * @param dateStr
     * @param sdr
     * @return
     */
    public static long dateStr2Timestamp(String dateStr,SimpleDateFormat sdr) {
        Date date;
        long timeStamp=0;
        try {
            date = sdr.parse(dateStr);
            timeStamp= date.getTime()/1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeStamp;
    }


    /**
     * 例:转化为 为yyyy-MM-dd HH:mm:ss 格式  dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     * ============================时间戳转换===========================
     * @param createTime 时间戳 10位(字符串)
     * @return
     */
    public static String getSimpleDateTime(String createTime,SimpleDateFormat dateFormat) {
        //将服务器换回的String 时间戳转换成long类型数据
        if (null==createTime||createTime.isEmpty()){
            return "";
        }
        if (createTime.length()!=10){
            return createTime;
        }
        try {
            long time = Long.parseLong(createTime);
            long issueTime = new Date(time * 1000).getTime();//发布时毫秒
            String timeStamp = dateFormat.format(new Date(issueTime));//例 yyyy-MM-dd HH:mm:ss 格式
            return timeStamp;
        }catch (NumberFormatException e){

        }
        return createTime;
    }

    /**
     * ============================时间戳转换===========================
     * 例:yyyy-MM-dd HH:mm:ss 格式
     * @param createTime 时间戳 13位 long
     * @return
     */
    public static String getSimpleDateTime(long createTime, SimpleDateFormat dateFormat) {
        if (createTime<=0){
            return "";
        }
        if (createTime+"".length()!=13){
            return createTime+"";
        }
        try {
            String timeStamp = dateFormat.format(new Date(createTime));//例 yyyy-MM-dd HH:mm:ss 格式
            return timeStamp;
        }catch (NumberFormatException e){
            Log.e(TAG, "getSimpleDateTime: "+e.toString() );
        }
        return createTime+"";
    }


    /**
     *  获取日期和星期
     * @param timeStamp 时间戳
     * @return 2021-01-01 星期三
     */
    public static String getDateAndWeek(long timeStamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd    EEEE", Locale.CHINA);
        String timeStr="";
        try {
            timeStr = sdf.format(timeStamp);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return timeStr;
    }

    /**
     * 获取日期和星期
     * @param date
     * @return 2021-01-01 星期三
     */
    public static String getDateAndWeek(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd    EEEE",Locale.CHINA);
        return sdf.format(date);
    }


    //获取今天是星期几
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 获取今天是星期几
     * @param time 10位时间戳
     * @return
     */
    public static String getWeekOfDate(long time) {
        Date date=new Date(time*1000);
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static int getYear(){
        Calendar calendar=Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    public static int getMonth(){
        Calendar calendar=Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH)+1;
        return month;
    }

    public static int getDay(){
        Calendar calendar=Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static int getHour(){
        Calendar calendar=Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    public static int getHour(long mills){
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(mills);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    public static int getMinute(){
        Calendar calendar=Calendar.getInstance();
        int min = calendar.get(Calendar.MINUTE);
        return min;
    }

    public static int getMinute(long mills){
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(mills);
        int min = calendar.get(Calendar.MINUTE);
        return min;
    }

    public static int getSecond(){
        Calendar calendar=Calendar.getInstance();

        int sec = calendar.get(Calendar.SECOND);
        return sec;
    }

    public static int getSecond(long mills){
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(mills);
        int sec = calendar.get(Calendar.SECOND);
        return sec;
    }

    /**
     * 获取参数时间距今多少天
     * @param time 天数
     * @return
     */
    public static int getBeforePresentDay(String time){
        if (TextUtils.isEmpty(time)){
            return 0;
        }
        if (time.length()!=10){
            return 0;
        }

        try {
            long bTime = Long.parseLong(time)*1000;
            long dayMills=1000*60*60*24;//一天的毫秒数
            long oldHour=getHour(bTime)*60*60*1000;
            long oldMin=getMinute(bTime)*60*1000;
            long oldSec=getSecond(bTime)*60*1000;

            long cTime = System.currentTimeMillis();//当前时间
            long vTime=cTime-bTime;
            long remain=vTime%dayMills;//余数
            int count= (int) (vTime/dayMills);
//            Log.e("cpdd", "getBeforePresentDay: "+oldHour+"***"+oldMin+"***"+oldSec );
            if(remain>(dayMills-oldHour-oldMin-oldSec)){
                //余数+设置时的时间超过设置时间晚上12点 算一天
                count+=1;
            }
            return count;
        }catch (Exception e){

        }
         return 0;
    }

    /**
     * 获取第二年情人节的时间戳
     * @return
     */
    public static long getNextValentineDayTimeStamp(){
        int year = getYear();
        long timestamp = dateStr2Timestamp( (year + 1) + "-02-14", new SimpleDateFormat("yyyy-MM-dd"));
        return timestamp*1000;
    }

}
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门