java获取当前时间戳转换

java获取当前时间戳转换 packagecom.pts.peoplehui.utils; importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.Locale; publicclassDateUtils{    publicstaticString…

大家好,又见面了,我是你们的朋友全栈君。

 
package com.pts.peoplehui.utils;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
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;
    }
}
 
 

微信关注 “安卓集中营”,获取更多

或者扫码关注

java获取当前时间戳转换一起共同学习探讨

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131977.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 有没有提醒喝水的打卡软件_小爱同学定时打卡

    有没有提醒喝水的打卡软件_小爱同学定时打卡本文方案来自CSDN博主「机灵鹤」方案详情可查看原文:https://smartcrane.blog.csdn.net/article/details/121837850本项目实现了一个久坐提醒和喝水提醒的小助手,在检测到连续工作较长时间之后,会打印纸条,提醒我们起身活动一下;在设定的喝水时间到了以后,也会打印纸条,提醒我们去喝水。项目用到的硬件设备有:硬件都备的差不多了,在开发时我遇到了一个比较棘手的问题,就是咕咕机跟人体传感器它也不是同一家的设备啊!虽然两家都有各自的控制APP,但是它们毕竟是两套系

    2022年10月1日
    3
  • 传真服务器的定义与选购

    传真服务器的定义与选购 AOFAX热点:传真服务器的定义与选购服务信息卡:联系人:朱成军服务热线:15920885998 商务QQ:909075483.一、传真服务器和传真系统传真服务器定义:所谓传真服务器,就是为企业内部所有员工提供电脑传真收发服务的设备,能够长期脱离其他电脑独立工作,是传真服务器的必要条件。传真服务器由传真软件、传真卡、工控机等组成。传真系统:传真服务器在某些场合也

    2022年6月28日
    27
  • SVN中如何执行clean up

    SVN中如何执行clean up

    2021年10月20日
    155
  • Ubuntu安装二:在VM中安装Ubuntu

    Ubuntu安装二:在VM中安装Ubuntu

    2021年12月3日
    51
  • 树莓派python编程指南_树莓派python软件

    树莓派python编程指南_树莓派python软件一:格式化SD卡SD卡插入读卡器连接电脑,使用SDFormatter对SD卡进行格式化(重装烧录也要进行着SD卡格式化操作)二:下载官方镜像http://downloads.raspberrypi.org/raspbian_latest三:烧录SD卡使用Win32DiskImager将镜像烧录到格式化后的SD卡SD卡盘根目录(/boot)下新建一个命名为ssh的…

    2022年10月15日
    3
  • 分治策略结合递归思想求最大子序列和

    分治策略结合递归思想求最大子序列和

    2021年12月6日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号