首页 > 编程知识 正文

当前时间转换成毫秒数,秒换成天数

时间:2023-05-06 05:31:37 阅读:238656 作者:2021

两个日期相差多少天:

public int days(String startDate,String endDate) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); long to = 0,from = 0; try { to = df.parse(endDate).getTime(); from = df.parse(startDate).getTime(); } catch (ParseException e) { e.printStackTrace(); } int num = (int) ((to - from) / (1000 * 60 * 60 * 24)); return num;}

毫秒转日期

public static String toData(long milliscond){ Date date = new Date(milliscond); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String data = simpleDateFormat.format(gc.getTime()); return data;}

日期转毫秒数:

/** * 将字符串数据转化为毫秒数 */public static long time(String dateTime) { String time1 = dateTime.replaceAll("-",""); String time2 = time1.replaceAll(":",""); String time3 = time2.replaceAll(" ",""); Calendar c = Calendar.getInstance(); try { c.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(time3)); } catch (ParseException e) { e.printStackTrace(); } long msTime = c.getTimeInMillis(); return msTime;}

获取当前系统时间:

long time = System.currentTimeMillis();

日期转换为星期:

/** * 根据日期转换为星期 * @param sDate * @return */public static String getFullDateWeekTime(String sDate){ try{ String formater = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat format = new SimpleDateFormat(formater); Date date=format.parse(sDate); format.applyPattern("yyyy-MM-dd E HH:mm:ss"); return format.format(date); }catch(Exception ex){ System.out.println("TimeUtil getFullDateWeekTime"+ex.getMessage()); return ""; }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。