主键ID、时间转化工具类
来源:互联网 发布:hifi音频播放软件 编辑:程序博客网 时间:2024/06/04 19:08
<span style="font-family: Arial, Helvetica, sans-serif;">利用uuid生成</span>
<span style="font-family: Arial, Helvetica, sans-serif;">String zzid = UUID.randomUUID().toString();</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;">利用时间生成</span>
<span style="font-family: Arial, Helvetica, sans-serif;">package ssm.utils;</span>
import java.util.Date;import java.util.Random;import java.text.SimpleDateFormat;public class IDGenUtils {private final static String DATA_FORMAT = "yyyyMMddHHmmssSSS";public static synchronized String gen(){Random random = new Random();SimpleDateFormat sf = new SimpleDateFormat(DATA_FORMAT);Date nowDate = new Date();return sf.format(nowDate) + random.nextInt(10) + random.nextInt(10) + random.nextInt(10)+ random.nextInt(10) + random.nextInt(10)+ random.nextInt(10)+ random.nextInt(10) + random.nextInt(10);}}
日期转换
package ssm.utils;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil { /** * 格式化输出日期 * @param date * 日期 * @param format * 格式 * @return 返回字符型日期 */ public static String format(Date date, String format) { String result = ""; try { if (date != null) { DateFormat df = new SimpleDateFormat(format); result = df.format(date); } } catch (Exception e) { } return result; } public static String format(Date date) { return format(date, "yyyy/MM/dd"); } /** * 返回年份 * * @param date * 日期 * @return 返回年份 */ public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /** * 返回月份 * * @param date * 日期 * @return 返回月份 */ public static int getMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MONTH) + 1; } /** * 返回日份 * * @param date * 日期 * @return 返回日份 */ public static int getDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_MONTH); } /** * 返回小时 * * @param date * 日期 * @return 返回小时 */ public static int getHour(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.HOUR_OF_DAY); } /** * 返回分钟 * * @param date * 日期 * @return 返回分钟 */ public static int getMinute(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MINUTE); } /** * 返回秒钟 * * @param date * 日期 * @return 返回秒钟 */ public static int getSecond(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.SECOND); } /** * 返回毫秒 * * @param date * 日期 * @return 返回毫秒 */ public static long getMillis(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.getTimeInMillis(); } /** * 返回字符型日期 * * @param date * 日期 * @return 返回字符型日期 */ public static String getDate(Date date) { return format(date, "yyyy/MM/dd"); } /** * 返回字符型时间 * * @param date * 日期 * @return 返回字符型时间 */ public static String getTime(Date date) { return format(date, "HH:mm:ss"); } /** * 返回字符型日期时间 * * @param date * 日期 * @return 返回字符型日期时间 */ public static String getDateTime(Date date) { return format(date, "yyyy/MM/dd HH:mm:ss"); } /** * 日期相加 * * @param date * 日期 * @param day * 天数 * @return 返回相加后的日期 */ public static Date addDate(Date date, int day) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000); return c.getTime(); } /** * 日期相减 * * @param date * 日期 * @param date1 * 日期 * @return 返回相减后的日期 */ public static int diffDate(Date date, Date date1) { return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000)); } /** * 取得指定月份的第一天 * * @param Date date * @return String */ public static String getMonthBegin(Date date) { return formatDateByFormat(date,"yyyy-MM") + "-01"; } /** * 取得指定日期月份的最后一天 * * @param Date date * @return String */ @SuppressWarnings("deprecation")public static Date getMonthEnd(Date date) { Calendar cDay1 = Calendar.getInstance(); cDay1.setTime(date); final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH); Date lastDate = cDay1.getTime(); lastDate.setDate(lastDay); return lastDate; } /** * 获取日期下个月的这一天 */ public static String getNextMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH,1); calendar.add(Calendar.DAY_OF_YEAR, -1); return formatDate(calendar.getTime()); } /** * 常用的格式化日期 * * @param date Date * @return String */ public static String formatDate(Date date) { return formatDateByFormat(date,"yyyy-MM-dd"); } /** * 以指定的格式来格式化日期 * * @param date Date * @param format String * @return String */ public static String formatDateByFormat(Date date,String format) { String result = ""; if(date != null) { try { SimpleDateFormat sdf = new SimpleDateFormat(format); result = sdf.format(date); } catch(Exception ex) { ex.printStackTrace(); } } return result; } /** *Date,String 间相互转换 */ public static Date stringToDate(String strDate){ DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = fmt.parse(strDate);} catch (ParseException e) {e.printStackTrace();}return date;} /** *计算年龄 */ public static int getBirthday(String strDate){ Date date = stringToDate(strDate); int birth_year = getYear(date); int local_year = getYear(new Date()); int age = local_year-birth_year; return age; }}
常用的日期转换
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");java.util.Date date = new java.util.Date();long time = date.getTime();//毫秒数format.parse(String);System.out.println(format.format(date));
0 0
- 主键ID、时间转化工具类
- java 时间转化工具类
- 时间转化工具类分享
- 时间日期工具类(相关转化)
- 时间戳在线转化工具
- JAVA工具类集锦2--时间格式转化类
- 通用主键id生成 和 系统时间格式转换
- 一个不错的jde时间转化工具
- 时间戳&date&String转化工具
- 时间日期相关工具类-获取星期,月份,时间戳转化等
- 常用类, 时间转化
- Json转化工具类
- 工具类-dp转化
- nhibernate实体类主键ID赋值问题
- 2.复合主键:工具类
- 生成唯一主键工具类
- Android 将时间转化成几分钟前、几天前等字样的工具类
- 主键id为null
- Linux Is Not Matrix——Nginx安装
- easyui中ajax笔记
- vs2013+Opencv2.4.9+MFC的图像浏览窗口
- cin>>i的返回值的问题 & cin输入流
- ComboBox 强制转换为大写的方法
- 主键ID、时间转化工具类
- 图片加载与缓存利器(自动缓存)--第三方开源-- Glide
- DataGridView 索引,添加列
- 读书笔记--Intent
- python 网络爬虫(一):世界上最简单的Web浏览器
- JAVA同步容器使用心得
- 归并排序
- online_judge_1135
- 151221 HAoi2015 总结