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

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