博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
时间类(时间戳的各种转换成)
阅读量:5260 次
发布时间:2019-06-14

本文共 12610 字,大约阅读时间需要 42 分钟。

public class Date_U {        /**         * 掉此方法输入所要转换的时间输入例如("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);                        Log.d("--444444---", times);                } catch (ParseException e) {                        e.printStackTrace();                }                return times;        }        /**         * 掉此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳         *         * @param time         * @return         */        public 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);                        Log.d("--444444---", times);                } catch (ParseException 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);                        Log.d("--444444---", times);                } catch (ParseException e) {                        e.printStackTrace();                }                return times;        }        /**         * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16时09分00秒")         *         * @param time         * @return         */        public 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分")         *         * @param time         * @return         */        public 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;                       }        // 调用此方法输入所要转换的时间戳例如(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;        }        /**         * 并用分割符把时间分成时间数组         *         * @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 (ParseException 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 (ParseException 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 (ParseException 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 (ParseException 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/Free-Thinker/p/6694722.html

你可能感兴趣的文章
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 文件操作
查看>>
Java 数组实例
查看>>
Java 方法实例
查看>>
Java 异常处理
查看>>
Java 目录
查看>>
Java 数据结构
查看>>
MYSQL5.7.24编译安装
查看>>
mysql启动过程
查看>>
应用Python来计算排列中的逆序数个数
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
关于拷贝构造函数与赋值构造函数的深刻解析
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
用原生JS获取非行间样式
查看>>
toolbox类
查看>>