美文网首页
安卓时间工具类

安卓时间工具类

作者: WeekDiffculty | 来源:发表于2017-03-06 14:57 被阅读0次
public class DateUtils {  
  
    public static String getTodayDateTime() {  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",  
                Locale.getDefault());  
        return format.format(new Date());  
    }  
  
    /** 
     * 掉此方法输入所要转换的时间输入例如("2014年06月14日16时09分00秒")返回时间戳 
     *  
     * @param time 
     * @return 
     */  
    public String data(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒",  
                Locale.CHINA);  
        Date date;  
        String times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.getTime();  
            String stf = String.valueOf(l);  
            times = stf.substring(0, 10);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return times;  
    }  
  
    public static String getTodayDateTimes() {  
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日",  
                Locale.getDefault());  
        return format.format(new Date());  
    }  
      
    /** 
     * 获取当前时间 
     *  
     * @return 
     */  
    public static String getCurrentTime_Today() {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
        return sdf.format(new java.util.Date());  
    }  
  
    /** 
     * 调此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳 
     *  
     * @param time 
     * @return 
     */  
    public static String dataOne(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",  
                Locale.CHINA);  
        Date date;  
        String times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.getTime();  
            String stf = String.valueOf(l);  
            times = stf.substring(0, 10);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return times;  
    }  
  
    public static String getTimestamp(String time, String type) {  
        SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);  
        Date date;  
        String times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.getTime();  
            String stf = String.valueOf(l);  
            times = stf.substring(0, 10);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return times;  
    }  
  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16时09分00秒") 
     *  
     * @param time 
     * @return 
     */  
    public static String times(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
      
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14  16:09:00") 
     *  
     * @param time 
     * @return 
     */  
    public static String timedate(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16:09") 
     *  
     * @param time 
     * @return 
     */  
    public static String timet(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static String timeslash(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd,HH:mm");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static String timeslashData(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
//      int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(lcc * 1000L));  
        return times;  
  
    }  
      
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static String timeMinute(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("HH:mm");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    public static String tim(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyyMMdd HH:mm");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
    }  
  
    public static String time(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
    }  
  
    // 调用此方法输入所要转换的时间戳例如(1402733340)输出("2014年06月14日16时09分00秒")  
    public static String times(long timeStamp) {  
        SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日  #  HH:mm");  
        return sdr.format(new Date(timeStamp)).replaceAll("#",  
                getWeek(timeStamp));  
  
    }  
  
    private static String getWeek(long timeStamp) {  
        int mydate = 0;  
        String week = null;  
        Calendar cd = Calendar.getInstance();  
        cd.setTime(new Date(timeStamp));  
        mydate = cd.get(Calendar.DAY_OF_WEEK);  
        // 获取指定日期转换成星期几  
        if (mydate == 1) {  
            week = "周日";  
        } else if (mydate == 2) {  
            week = "周一";  
        } else if (mydate == 3) {  
            week = "周二";  
        } else if (mydate == 4) {  
            week = "周三";  
        } else if (mydate == 5) {  
            week = "周四";  
        } else if (mydate == 6) {  
            week = "周五";  
        } else if (mydate == 7) {  
            week = "周六";  
        }  
        return week;  
    }  
  
    // 并用分割符把时间分成时间数组  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14-16-09-00") 
     *  
     * @param time 
     * @return 
     */  
    public String timesOne(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    public static String timesTwo(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  
  
    }  
  
    /** 
     * 并用分割符把时间分成时间数组 
     *  
     * @param time 
     * @return 
     */  
    public static String[] timestamp(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        String[] fenge = times.split("[年月日时分秒]");  
        return fenge;  
    }  
  
    /** 
     * 根据传递的类型格式化时间 
     *  
     * @param str 
     * @param type 
     *            例如:yy-MM-dd 
     * @return 
     */  
    public static String getDateTimeByMillisecond(String str, String type) {  
  
        Date date = new Date(Long.valueOf(str));  
  
        SimpleDateFormat format = new SimpleDateFormat(type);  
  
        String time = format.format(date);  
  
        return time;  
    }  
  
    /** 
     * 分割符把时间分成时间数组 
     *  
     * @param time 
     * @return 
     */  
    public String[] division(String time) {  
  
        String[] fenge = time.split("[年月日时分秒]");  
  
        return fenge;  
  
    }  
  
    /** 
     * 输入时间戳变星期 
     *  
     * @param time 
     * @return 
     */  
    public static String changeweek(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        Date date = null;  
        int mydate = 0;  
        String week = null;  
        try {  
            date = sdr.parse(times);  
            Calendar cd = Calendar.getInstance();  
            cd.setTime(date);  
            mydate = cd.get(Calendar.DAY_OF_WEEK);  
            // 获取指定日期转换成星期几  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        if (mydate == 1) {  
            week = "星期日";  
        } else if (mydate == 2) {  
            week = "星期一";  
        } else if (mydate == 3) {  
            week = "星期二";  
        } else if (mydate == 4) {  
            week = "星期三";  
        } else if (mydate == 5) {  
            week = "星期四";  
        } else if (mydate == 6) {  
            week = "星期五";  
        } else if (mydate == 7) {  
            week = "星期六";  
        }  
        return week;  
  
    }  
  
    /** 
     * 获取日期和星期 例如:2014-11-13 11:00 星期一 
     *  
     * @param time 
     * @param type 
     * @return 
     */  
    public static String getDateAndWeek(String time, String type) {  
        return getDateTimeByMillisecond(time + "000", type) + "  "  
                + changeweekOne(time);  
    }  
  
    /** 
     * 输入时间戳变星期 
     *  
     * @param time 
     * @return 
     */  
    public static String changeweekOne(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        Date date = null;  
        int mydate = 0;  
        String week = null;  
        try {  
            date = sdr.parse(times);  
            Calendar cd = Calendar.getInstance();  
            cd.setTime(date);  
            mydate = cd.get(Calendar.DAY_OF_WEEK);  
            // 获取指定日期转换成星期几  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        if (mydate == 1) {  
            week = "星期日";  
        } else if (mydate == 2) {  
            week = "星期一";  
        } else if (mydate == 3) {  
            week = "星期二";  
        } else if (mydate == 4) {  
            week = "星期三";  
        } else if (mydate == 5) {  
            week = "星期四";  
        } else if (mydate == 6) {  
            week = "星期五";  
        } else if (mydate == 7) {  
            week = "星期六";  
        }  
        return week;  
  
    }  
  
    /** 
     * 获取当前时间 
     *  
     * @return 
     */  
    public static String getCurrentTime() {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
        return sdf.format(new java.util.Date());  
    }  
      
    /** 
     * 输入日期如(2014年06月14日16时09分00秒)返回(星期数) 
     *  
     * @param time 
     * @return 
     */  
    public String week(String time) {  
        Date date = null;  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
        int mydate = 0;  
        String week = null;  
        try {  
            date = sdr.parse(time);  
            Calendar cd = Calendar.getInstance();  
            cd.setTime(date);  
            mydate = cd.get(Calendar.DAY_OF_WEEK);  
            // 获取指定日期转换成星期几  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        if (mydate == 1) {  
            week = "星期日";  
        } else if (mydate == 2) {  
            week = "星期一";  
        } else if (mydate == 3) {  
            week = "星期二";  
        } else if (mydate == 4) {  
            week = "星期三";  
        } else if (mydate == 5) {  
            week = "星期四";  
        } else if (mydate == 6) {  
            week = "星期五";  
        } else if (mydate == 7) {  
            week = "星期六";  
        }  
        return week;  
    }  
  
    /** 
     * 输入日期如(2014-06-14-16-09-00)返回(星期数) 
     *  
     * @param time 
     * @return 
     */  
    public String weekOne(String time) {  
        Date date = null;  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
        int mydate = 0;  
        String week = null;  
        try {  
            date = sdr.parse(time);  
            Calendar cd = Calendar.getInstance();  
            cd.setTime(date);  
            mydate = cd.get(Calendar.DAY_OF_WEEK);  
            // 获取指定日期转换成星期几  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        if (mydate == 1) {  
            week = "星期日";  
        } else if (mydate == 2) {  
            week = "星期一";  
        } else if (mydate == 3) {  
            week = "星期二";  
        } else if (mydate == 4) {  
            week = "星期三";  
        } else if (mydate == 5) {  
            week = "星期四";  
        } else if (mydate == 6) {  
            week = "星期五";  
        } else if (mydate == 7) {  
            week = "星期六";  
        }  
        return week;  
    }  
}  

相关文章

  • 安卓时间工具类

  • 安卓工具类

    原文:https://www.cnblogs.com/AbrahamCaiJin/p/7154625.html A...

  • Android 常用开源库(四)

    其他 5.1工具类 AndroidUtilCode 强大易用的安卓工具类库 RxTools Android开发人员...

  • 安卓权限工具类

    安卓6.0后敏感权限需要动态申请,近期对之前的权限工具类做了整理和改进。目标,让敲代码更加可控的ctrl c.此...

  • 安卓尺寸工具类

    DimensionUtil - 尺寸工具类 简介 DimensionUtil是一个用于尺寸转换的工具类,主要的用途...

  • AndroidUtilCode 最强大的工具类

    一、前言: AndroidUtilCode 是一个强大易用的安卓工具类库,它合理地封装了安卓开发中常用的函数,具...

  • 【8】Service

    一、什么是Service? Service是安卓的服务类,是一种没有界面的工具类,可以长时间在后台运行,用来执行一...

  • RN获取缓存

    需求如图 封装一个缓存的工具类 调用 安卓代码 ios端代码

  • AndroidUtilCode 工具类

    一、简介:AndroidUtilCode 是一个强大易用的安卓工具类库,它合理地封装了安卓开发中常用的函数,具有完...

  • 安卓SharePreference工具类封装

    这个工具的封装是采用单列模式,将类本身进行实例化,在构造方法里面进行SharePreference初始化,然后在s...

网友评论

      本文标题:安卓时间工具类

      本文链接:https://www.haomeiwen.com/subject/hjcugttx.html