java常用类--日期和时间
来源:互联网 发布:淘宝客营销案例 编辑:程序博客网 时间:2024/06/05 18:06
java日期和时间
java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。
第一个构造函数使用当前日期和时间来初始化对象。
Date()
第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数。
Date(longmillisec)
/** * custom format */ public static final String FORMAT_0 = "yyyy-MM-dd HH:mm:ss"; /** * get current timeStamp * * @return */ public static long getCurrentStamp() { return System.currentTimeMillis(); } /** * get current time * * @return */ public static String getCurrentDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(new Date()); return time; } /** * from date to stamp * * @param s * @return * @throws ParseException */ public static String dateToStamp(String s) throws ParseException { String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(s); long ts = date.getTime(); res = String.valueOf(ts); return res; } /** * from stamp to date * * @param s * @return */ public static String stampToDate(String s) { String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(s); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; } /** * update time * * @param ctime submit time * @return */ public static String showTime(Date ctime) { String r = ""; if (ctime == null) return r; long nowtimelong = System.currentTimeMillis(); long ctimelong = ctime.getTime(); long result = Math.abs(nowtimelong - ctimelong); if (result < 60000) {// 一分钟内 long seconds = result / 1000; if (seconds == 0) { r = "刚刚"; } else { r = seconds + "秒前"; } } else if (result >= 60000 && result < 3600000) {// 一小时内 long seconds = result / 60000; r = seconds + "分钟前"; } else if (result >= 3600000 && result < 86400000) {// 一天内 long seconds = result / 3600000; r = seconds + "小时前"; } else if (result >= 86400000 && result < 1702967296) {// 一个月内 long seconds = result / 86400000; r = seconds + "天前"; } else {// 日期格式 String format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat df = new SimpleDateFormat(format); r = df.format(ctime).toString(); } return r; } /** * The number of days to get two dates * * @param newTime * @param oldTime * @return */ public static long getDays(String newTime, String oldTime) { if (newTime == null || newTime.equals("")) return 0; if (oldTime == null || oldTime.equals("")) return 0; // 转换为标准时间 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); Date newDate = null; Date oldDate = null; try { newDate = myFormatter.parse(newTime); oldDate = myFormatter.parse(oldTime); } catch (Exception e) { } long day = (newDate.getTime() - oldDate.getTime()) / (24 * 60 * 60 * 1000); return day; } private static long midTime; /** * countDownTime * * @param limitTimeStamp */ public static void countDownTime(long limitTimeStamp) { //limitTime:1 calendar Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2017); calendar.set(Calendar.MONTH, 10);// 注意月份的设置,0-11表示1-12月 calendar.set(Calendar.DAY_OF_MONTH, 20); calendar.set(Calendar.HOUR_OF_DAY, 20); calendar.set(Calendar.MINUTE, 49); calendar.set(Calendar.SECOND, 0); long endTime = calendar.getTimeInMillis(); //2 limtStamp endTime = limitTimeStamp; Date date = new Date(); long startTime = date.getTime(); midTime = (endTime - startTime) / 1000; final Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { if (midTime > 0) { midTime--; long hh = midTime / 60 / 60 % 60; long mm = midTime / 60 % 60; long ss = midTime % 60;// Message message = new Message();// message.obj = "还剩" + hh + "小时" + mm + "分钟" + ss + "秒";// handler.sendMessage(message); } else {// Message message = new Message();// message.obj = "还剩" + 0 + "小时" + 0 + "分钟" + 0 + "秒";// handler.sendMessage(message); timer.cancel(); } } }, 0, 1000); }
/* * 将时间转换为时间戳 */ public static String dateToStamp(String s) throws ParseException{ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(s); long ts = date.getTime(); res = String.valueOf(ts); return res; }
时间戳转换为时间:
/* * 将时间戳转换为时间 */ public static String stampToDate(String s){ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(s); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; }
阅读全文
0 0
- java常用类--日期和时间
- JAVA基础:常用String类和时间日期类
- Java常用类:时间日期类
- 深入理解Java常用类-----时间日期
- java日期和时间
- java 日期和时间
- java 日期和时间
- Java日期和时间
- JAVA日期和时间类彻底解决(2)
- JAVA日期和时间类彻底解决(3)
- Java日期和时间类简介
- JAVA处理日期时间常用方法:强大的calendar类
- java开发中常用的日期时间工具类 DateUtil
- Java日期时间类
- java获取日期和时间
- 11.java 时间和日期
- java的日期和时间
- java格式化日期和时间
- host头攻击解决方法
- byte数组与文件的转换
- Git语句
- 堆排序算法Java面向对象实现
- unity中全息投影带贴图shader
- java常用类--日期和时间
- 【机器学习】归一化
- Java入门学习(一)
- 安卓打包遇到的问题
- java基础之运算符
- LintCode245子树
- Unity Shader 学习笔记(15) 立方体纹理、反射、折射、菲涅尔反射
- 整理几篇关于g2o的博客
- DeepDive学习