Android 之Date常见的方法使用

导读:本篇文章讲解 Android 之Date常见的方法使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  1. /**
    • 描述:Date类型转化为String类型.
    • @param date the dateo
    • @param format 类型如 yyyy-MM-dd HH:mm:ss
      */
    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        String strDate = null;
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
  1. /**获取当前时间
    • pattern 类型 yyyy-MM-dd HH:mm:ss
    • @return
      */
    public static String getCurrentDate(String pattern) {

        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String timestamp = formatter.format(curDate);

        return timestamp;
    }
  1. /**
    • 获取当前日期的day天后的日期
    • @param pattern 格式类型
    • @param day 天数
    • @return
      */
    public static String getNewData(String pattern, int day) {
        Format format = new SimpleDateFormat(pattern);
        long time = 0;
        Date today = new Date();
        time = (today.getTime() / 1000) + 60 * 60 * 24 * day;
        Date newDate = new Date();
        newDate.setTime(time * 1000);
        String date = format.format(newDate);
        return date;
    }

/**
 * 
 * 
 * @param DATE1  判断日期是否超过当前日期  超过当前日期为true
 * @return
 */
    public static boolean compare_date(String DATE1) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = new Date();
            if (dt1.getTime() > dt2.getTime()) {//传入时间小于当前时间  4.7大于4.8
                System.out.println("还没到...");
                return false;
            } else if (dt1.getTime() <= dt2.getTime()) {
                System.out.println("超过当期日期...");
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }
  1. /** 描述:比较两个日期的大小
    • old 表示之前的时间
    • now 现在的时间
    • @return
      */
    public static String DateCompare(String old,String now){
        java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Calendar c1=java.util.Calendar.getInstance();
        java.util.Calendar c2=java.util.Calendar.getInstance();
        try {
            c1.setTime(df.parse(old));
            c2.setTime(df.parse(now));
        } catch (Exception e) {
            System.out.println("格式不正确");
            e.printStackTrace();
        }
        int result=c1.compareTo(c2);
        if(result==0){
            return "相等";
        }else if(result<0){
            return "小于";
        }else
        return "大于";
    }
  1. /**描述
    • 根据出生日期获得年龄
      • data 输入的生日 pattern 转换的类型 如:yyyyMM-dd
    • @return
      */
public static String getAge(String data,String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date curDate = new Date();//
        String timestamp = formatter.format(curDate);
        String brith = data.substring(0, 4);
        String now = timestamp.substring(0, 4);
        int age = Integer.valueOf(now) - Integer.valueOf(brith);
        return age + "岁";
    }
  1. //通过String类型的日期判断是星期几
 /**
 * 
 * 根据String 类型的日期判断是星期几??
 * @param date     String 类型的日期 
 * @param format   格式类型
 * @return
 */
    public static String getWeekOfDate(String date, String format) {
        String[] dayofweek = new String[] { "星期日", "星期一", "星期二", "星期三", "星期四",
                "星期五", "星期六" };
        Calendar c = Calendar.getInstance();
        try {
            c.setTime((new SimpleDateFormat(format)).parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dayofweek[c.get(Calendar.DAY_OF_WEEK) - 1];
    }

1.把String类型转换为Date类型

// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒,
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }


/**
*
* 项目中用的这个方法比较仿扣扣评论发送时间
* @param startDate 开始日期
* @param endDate 结束日期
* @return
*/

public static String twoDateDistance(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        } else {
            long timeLong = endDate.getTime() - startDate.getTime();
            if (timeLong < 60 * 1000) {
                return "刚刚";

            } else if (timeLong < 60 * 60 * 1000) {
                timeLong =timeLong/1000/60;
                return timeLong+"分钟前";

            }else if(timeLong<60*60*24*1000){
                timeLong = timeLong/60/60/1000; 
                return timeLong+"小时前";

            }else if(timeLong<60*60*24*1000*7){
                timeLong = timeLong/1000/ 60 / 60 / 24;
                return timeLong+"天前";
            }else if(timeLong<60*60*24*1000*7*4){
                timeLong = timeLong/1000/ 60 / 60 / 24/7;
                return timeLong+"周前";

            }
            else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                 return  sdf.format(startDate);
            }
        }

    }
    private static String strYear;
    private static String strMonth;
    private static String strDay;
    private static String sex;
/**
 * 
 * @param mesg     根据生份证号获取生日
 * @return
 */
    public static String SetbirthDay(String mesg){
        if(mesg!=null&&mesg!=""){

            strYear = mesg.substring(6, 10);
            strMonth = mesg.substring(10, 12);
            strDay = mesg.substring(12, 14);

        }
        return strYear+"-"+strMonth+"-"+strDay;

    }
/**
 * 
 * @param mesg  根据省份证号获取性别
 * @return
 */

    public static String setSex(String mesg){
        if(mesg!=null &&mesg!=""){

            sex = mesg.substring(16, 17);
        }
        if(Integer.parseInt(sex)%2==0){
            sex = "女";
        }else{
            sex ="男";

        }
        return sex;

    }


/**
     * 根据传入的count数,来获取count天后的日期集合
     * 
     * @param count     表示要获取多少天数
     * @param flag   默认false
     * @return
     */
    public static List<String> getDateTimeStr(int count, boolean flag) {
        List<String> timestr = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            String dateStr = getDateStr(i + 1, flag);
            timestr.add(dateStr);
        }
        return timestr;
    }

    public static String getDateStr(int mon, boolean flag) {
        String time = "";
        if (flag) {
            if (mon % 2 == 1) {
                time = "01 ";
                mon = mon / 2 + 1;
            } else if (mon % 2 == 0) {
                time = "02 ";
                mon = mon / 2;
            }
        }
        Calendar cal = Calendar.getInstance();
        int _year = cal.get(Calendar.YEAR);
        int _month = cal.get(Calendar.MONTH) + 1;
        int _day = cal.get(Calendar.DAY_OF_MONTH) + mon;
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour > 23) {
            _day = _day + 1;
        }
        int lastday = getLastDay();
        if (_day > lastday) {
            _month = _month + 1;
            _day = _day - lastday;
        }
        String month = "";
        String day = "";
        if (_month < 10) {
            month = "0" + _month;
        } else {
            month = _month + "";
        }
        if (_day < 10) {
            day = "0" + _day;
        } else {
            day = _day + "";
        }
        if (_month > 12) {
            month = "01";
            _year = _year + 1;
        }
        time = time + _year + "-" + month + "-" + day;
        return time;
    }

    public static int getLastDay() {
        int lastday;
        Calendar d = Calendar.getInstance();
        int month = d.get(Calendar.MONTH);
        do {
            lastday = d.get(Calendar.DAY_OF_MONTH);
            d.add(Calendar.DAY_OF_MONTH, 1);
        } while (d.get(Calendar.MONTH) == month);
        return lastday;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12888.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!