zoukankan      html  css  js  c++  java
  • Date工具类

    总结了下项目中常用的时间转化方法,目前就这么点啦,以后再慢慢添加,先储备起来,免得丢啦。

    package com.example.keranbin.testdemo;
    
    import android.util.Log;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * Created by keranbin on 2016/8/1.
     */
    public class DateHelper {
        /*
        * 获取当前时间时间戳
        * */
        public static Long getTimeStamp() {
            //方法 一  :建议,最快
            return System.currentTimeMillis();
            //方法 二  :最慢,不建议使用
            //  return Calendar.getInstance().getTimeInMillis();
            //方法 三
            //  return  new Date().getTime();
        }
    
        /*
        * 获取当前年份
        * */
        public static int getYear() {
            return Calendar.getInstance().get(Calendar.YEAR);
        }
    
        /*
        * 获取当前月份
        * */
        public static int getMonth() {
            return Calendar.getInstance().get(Calendar.MONTH);
        }
    
        /*
        * 获取当月的第几天,从1开始
        * */
        public static int getDayOfMonth() {
            return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        }
    
        /*
        * 获取当年的第几天,从1开始
        * */
        public static int getDayOfYear() {
            return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        }
    
        /*
       * 获取这周的第几天,返回周几
       * */
        public static String getDayOfWeek() {
            return returnWeekStr(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
        }
    
        /*
        * 判断传入的时间戳是否是当年
        * */
        public static boolean isThisYear(String strNum) {
            if (strNum != null && !strNum.equals("")) {
                String strYear = new SimpleDateFormat("yyyy").format(new Date(Long.parseLong(strNum)));
                if (strYear.equals(String.valueOf(getYear()))) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
    
        /*
       * 判断传入的时间戳是否是当前月份
       * */
        public static boolean isThisMonth(String strNum) {
            if (strNum != null && !strNum.equals("")) {
                String strYear = new SimpleDateFormat("yyyy").format(new Date(Long.parseLong(strNum)));
                if (strYear.equals(String.valueOf(getMonth()))) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
    
        /*
      * 判断传入的时间戳是否是当前月份的今天
      * */
        public static boolean isThisDay(String strNum) {
            if (strNum != null && !strNum.equals("")) {
                String strYear = new SimpleDateFormat("yyyy").format(new Date(Long.parseLong(strNum)));
                if (strYear.equals(String.valueOf(getDayOfMonth()))) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
    
        /*
        * 按照输入的格式转化传入的时间戳
        * */
        public static String getDateTimeByTimeFormat(String num, String timeFormat) {
            if (!num.equals("null") && !num.equals(""))
                return new SimpleDateFormat(timeFormat).format(new Date(Long.parseLong(num)));
            return "";
        }
    
        /*
      * 按照返回输入的格式时间
      * */
        public static String getNowDateTimeByTimeFormat(String timeFormat) {
            return new SimpleDateFormat(timeFormat).format(new Date());
        }
    
        /*
        * 判断传入的两个时间相差几天
        * */
        public static String getDifferDays(Date date1, Date date2) {
            return String.valueOf((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
        }
    
        /*
        * 判断传入的时间戳是周几
        * */
        public static String getWeekStr(String strNum) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(Long.parseLong(strNum)));
            int intWeek = calendar.get(Calendar.DAY_OF_WEEK);
            return  returnWeekStr(intWeek);
        }
    
        /*
        * 根据传入的数据返回周几
        * */
        public static String  returnWeekStr(int intWeek){
            String strWeek="";
            switch (intWeek) {
                case 1:
                    strWeek = "星期日";
                    break;
                case 2:
                    strWeek = "星期一";
                    break;
                case 3:
                    strWeek = "星期二";
                    break;
                case 4:
                    strWeek = "星期三";
                    break;
                case 5:
                    strWeek = "星期四";
                    break;
                case 6:
                    strWeek = "星期五";
                    break;
                case 7:
                    strWeek = "星期六";
                    break;
            }
            return strWeek;
        }
    }
  • 相关阅读:
    C/C++语言void及void指针深层探索(转)
    Linux C++编程中的正则表达式使用范例
    正则表达式的基本概念和原理
    Web前端,高性能优化
    python爬虫练习2苏宁图书信息
    tensorflow鸢尾花分类
    在线编辑word文档 可保存到服务器
    如何取得DataGrid绑定列和模板列中的值
    ComponetOne C1WebChart使用精华
    C#多线程使用进度条
  • 原文地址:https://www.cnblogs.com/bdsdkrb/p/5728267.html
Copyright © 2011-2022 走看看