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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • win7电脑卡顿严重解决方法_电脑卡顿一下

    win7电脑卡顿严重解决方法_电脑卡顿一下最近有很多游戏玩家和小编说win7电脑玩2D游戏经常发生卡顿,游戏卡顿问题确实让玩家苦恼,因为导致2D游戏经常发生卡顿的原因有很多种,那么win7电脑玩2D游戏经常发生卡顿怎么办?不要着急,针对此问题,下面小编告诉大家造成2D游戏经常发生卡顿的六大解决方法。解决方法一:1、建议下载并安装【百度电脑专家】,2、打开【百度电脑专家】,在搜索框内输入“玩游戏卡”,在搜索结果里面选择第一项的【立即修复】,…

    2022年9月11日
    0
  • idea配置lombok.jar(idea安装离线插件)

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get、set、toString、equals和hashCode方法,虽然eclipse和idea开发环境下都有自动生成的快捷方式,但自动生成这些代码后,如果bean中的属性一旦有修改、删除或增加时,需要重新生成或删除get/set等方法,给代码维护增加负担。而使用了lombok则不一样,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHas

    2022年4月14日
    272
  • 获得焦点与失去焦点事件

    获得焦点与失去焦点事件一介绍获得焦点事件(onfocus)是当某个元素获得焦点时触发事件处理程序。失去焦点事件(onblur)是当前元素失去焦点时触发事件处理程序。一般情况下,这两个事件是同时使用的。 二应用文本框获得焦点时改变背景颜色本示例是在用户选择页面中的文本框时,改变文本框的背景颜色,当选择其他文本框时,将失去焦点的文本框背景颜色恢复原始状态。 三代码

    2022年6月29日
    92
  • hive数据类型转换cast_c语言数据类型的转换

    hive数据类型转换cast_c语言数据类型的转换在《Hive内置数据类型》文章中,我们提到了Hive内置数据类型由基本数据类型和复杂数据类型组成。今天的话题是Hive数据类型之间的转换。同Java语言一样,Hive也包括隐式转换(implicitconversions)和显式转换(explicitlyconversions)。  Hive在需要的时候将会对numeric类型的数据进行隐式转换。比如我们对两个不同数据类型的数字进行比较

    2022年9月21日
    0
  • linux修改密码报错_linux修改其他用户密码

    linux修改密码报错_linux修改其他用户密码第一种:/usr/bin/passwd的权限中没有添加s即SUID特殊权限即:-rwxr-xr-x.1rootroot270008月222010/usr/bin/passwd解决方案:chmodu+s/usr/bin/passwdSUID的功能简单的说就是让组用户或其他用户在执行该文件是拥有文件所有者(own)权限,这里就是需要获取w(写)权限,这样才能将新密码写到/etc…

    2022年9月5日
    1
  • php的Allowed memory size of 134217728 bytes exhausted问题解决办法

    php的Allowed memory size of 134217728 bytes exhausted问题解决办法php的Allowed memory size of 134217728 bytes exhausted问题解决办法

    2022年4月24日
    46

发表回复

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

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