栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

时间转换 java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

时间转换 java

    public Float getDuration(String start, String end){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        long startMilli = LocalDateTime.parse(start, df).toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long endMilli = LocalDateTime.parse(end, df).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//        long day = 24*60*60*1000; 转换为天
        long day = 60*60*1000;
        return (float)(endMilli - startMilli)/day;
    }


private Date stringToDate10(String date){
    Timestamp timestamp = new Timestamp(Long.valueOf(date) * 1000);
    return timestamp;
}



private Date stringToDate_(String date){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date strtodate = null;
    try {
        strtodate = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return strtodate;
}



private Date stringToDateTime(String date){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date strtodate = null;
    try {
        strtodate = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return strtodate;
}



private Date stringToDate(int date){
    String s = String.valueOf(date);
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = format.parse(s, pos);
    return strtodate;
}



private long getMonthFirst(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    // 本月起始
    Calendar thisMonthFirstDateCal = Calendar.getInstance();
    thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
    String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
       try{
               date = sf.parse(thisMonthFirstTime);
          } catch(ParseException e) {
               e.printStackTrace();
          }
    long time = date.getTime();
    return time;
}



private long getMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    // 本月起始
    Calendar thisMonthFirstDateCal = Calendar.getInstance();
    thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, thisMonthFirstDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
    String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    try{
        date = sf.parse(thisMonthFirstTime);
    } catch(ParseException e) {
        e.printStackTrace();
    }
    long time = date.getTime();
    return time;
}



private long getLastMonthFirst(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    //获取前月的第一天
    Calendar cal_1=Calendar.getInstance();//获取当前日期
    cal_1.add(Calendar.MONTH, -1);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
    String  firstDay = format.format(cal_1.getTime()) + " 00:00:00";

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    try{
        date = sf.parse(firstDay);
    } catch(ParseException e) {
        e.printStackTrace();
    }
    long time = date.getTime();
    return time;
}



private long getLastMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    //获取前月的最后一天
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
    String lastDay = format.format(cale.getTime()) + " 00:00:00";

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    try{
        date = sf.parse(lastDay);
    } catch(ParseException e) {
        e.printStackTrace();
    }
    long time = date.getTime();
    return time;
}



private long getToDay(){
    Date date = new Date();
    return date.getTime();
}



private int getMonthFirst(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar instance = Calendar.getInstance();
    instance.add(Calendar.MONTH,0);
    instance.set(Calendar.DAY_OF_MONTH,1);
    Date time = instance.getTime();
    String format1 = format.format(time);
    int from = 0;
    try {
        from = (int)(format.parse(format1).getTime()/1000);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return from;
}



private int getMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar ca = Calendar.getInstance();
    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
    String last = format.format(ca.getTime());
    int to = 0;
    try {
        to = (int)(format.parse(last).getTime()/1000);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return to;
}



private int getLastMonthFirst(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    //获取前月的第一天
    Calendar   cal_1=Calendar.getInstance();//获取当前日期
    cal_1.add(Calendar.MONTH, -1);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
    String  firstDay = format.format(cal_1.getTime());
    int from = 0;
    try {
        from = (int)(format.parse(firstDay).getTime()/1000);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return from;
}



private int getLastMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    //获取前月的最后一天
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
    String lastDay = format.format(cale.getTime());
    int to = 0;
    try {
        to = (int)(format.parse(lastDay).getTime()/1000);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return to;
}



private int getToDay(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    String time = format.format(date);
    int to = 0;
    try {
        to = (int)(format.parse(time).getTime()/1000);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return to;
}



private String getToDay(){
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Date date = new Date();
    String to = format.format(date);
    return to;
}


private String getMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Calendar ca = Calendar.getInstance();
    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
    String last = format.format(ca.getTime());
    return last;
}


private String getLastMonthFirst(){
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    //获取前月的第一天
    Calendar   cal_1=Calendar.getInstance();//获取当前日期
    cal_1.add(Calendar.MONTH, -1);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
    String  firstDay = format.format(cal_1.getTime());
    return firstDay;
}


private String getLastMonthFinal(){
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    //获取前月的最后一天
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
    String lastDay = format.format(cale.getTime());
    return lastDay;
}


 * 13位时间戳 转date
 * @param date
 * @return
 */
private Date stringToDate13(Long date){
    Timestamp timestamp = new Timestamp(date);
    return timestamp;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号