Android获取各式时间类型

在项目中需要获取各式各样的时间,而且也会有很多地方会调用到这些方法,所以我将它们归集于一个文件中,方便之后的使用。

现在我们需要了解一些相对基础获取时间的方法。

1.获取当前日历对象:

Calendar calendar = Calendar.getInstance();

2.获取当前时区下日期时间对应的时间戳:

calendar.getTimeInMillis();

3.获取标准格林尼治时间下日期时间对应的时间戳:

long unixTime = calendar.getTimeInMillis();
unixTime - TimeZone.getDefault().getRawOffset();

4.获取当前日期对象:

Date date = new Date();

5.获取当前时区下日期时间对应的时间戳:

date.getTimeInMillis();

6.设置日期时间格式:

SimpleDateFormat format = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

7.获取当前时区下日期时间对应的时间戳:

format.format(date);

现在来实现具体的方法。

1.获取时间戳:

 public static long getTime() {
        Calendar calendar = Calendar.getInstance();// 获取当前日历对象
        long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳
        return unixTime;
    }

2.获取标准的时间:

 public static String getStandardTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        return formatter.format(curDate);
    }

3.获取与现在时间的时间差(秒):

public static int getDurationSecond(String time) {
        int durationSecond = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = df.parse(time);
            MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());
            durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);
        } catch (Exception e) {
            MyLog.e("TimeUtils getDurationSecond error=" + e);
        }
        return durationSecond;
    }

4.获取时间差:

public static String getDuration(String one, String two) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = df.parse(two);
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = hr + "小时" + min + "分钟";
            } else {
                duration = l + "分钟";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return duration;
    }

完整代码展示:

public class MyTimeUtils {
  
    //获取时间戳
    public static long getTime() {
        Calendar calendar = Calendar.getInstance();// 获取当前日历对象
        long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳
        return unixTime;
    }

    public static String getTimeString() {
        return Long.toString(new Date().getTime());
    }

    //获取标准时间
    public static String getStandardTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        return formatter.format(curDate);
    }

    // 获取与现在时间的时间差(秒)
    public static int getDurationSecond(String time) {
        int durationSecond = 0;
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date;
        try {
            date = df.parse(time);
            MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());
            durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);
        } catch (Exception e) {
            MyLog.e("TimeUtils getDurationSecond error=" + e);
        }
        return durationSecond;
    }


    // 获取时间差
    public static String getDuration(String one, String two) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span><span style="font-family: SimHei;">);</span>
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = df.parse(two);
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span>;
            } else {
                duration = <span style="font-family: SimHei;">l + "分钟";</span>
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return duration;
    }

    // 获取与当前时间差
    public static String getcurDuration(String one) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = new Date();
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span><span style="font-family: SimHei;">;</span>
            } else {
                duration =<span style="font-family: SimHei;"> l + "分钟";</span>

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return duration;
    }

    /**
     * @return格式化当前日期和时间为字符串
     */
    public static String mCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        String currenttime = df.format(new Date());
        return currenttime;
    }

    public static String parseBangTime(long time) {
        MyLog.out("time==>" + time);
        String timeTemp = "";
        if (time < 60) {
            timeTemp = time + BaseApplication.getInstance().getString(R.string.seconds_before);
        } else if (time < (60 * 60)) {
            timeTemp = time / 60 + BaseApplication.getInstance().getString(R.string.minutes_before);
        } else if (time < (3600 * 24)) {
            timeTemp = time / 3600 + BaseApplication.getInstance().getString(R.string.hour_before);
        } else if (time < (60 * 60 * 24 * 30)) {
            timeTemp = time / (3600 * 24) + BaseApplication.getInstance().getString(R.string.today_before);
        } else {
            timeTemp = time / (3600 * 24 * 30) + BaseApplication.getInstance().getString(R.string.month_before);
        }
        return timeTemp;
    }

    public static String getTimeStamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_two));
        String timeStamp = dateFormat.format(new Date());
        MyLog.e("getTimeStamp=" + timeStamp);
        return timeStamp;
    }

    public static String getCurrentDate(){
        SimpleDateFormat df = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show));
        String currentDate = df.format(new Date());
        return currentDate;
    }
}

 

 

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值