日期的操作类
来源:互联网 发布:韩国打榜用什么软件 编辑:程序博客网 时间:2024/06/07 15:29
package 自定义包名;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * 日期操作工具类. * * */public class DateUtil {private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";public static Date str2Date(String str) {return str2Date(str, null);}public static Date str2Date(String str, String format) {if (str == null || str.length() == 0) {return null;}if (format == null || format.length() == 0) {format = FORMAT;}Date date = null;try {SimpleDateFormat sdf = new SimpleDateFormat(format);date = sdf.parse(str);} catch (Exception e) {e.printStackTrace();}return date;}public static Calendar str2Calendar(String str) {return str2Calendar(str, null);}public static Calendar str2Calendar(String str, String format) {Date date = str2Date(str, format);if (date == null) {return null;}Calendar c = Calendar.getInstance();c.setTime(date);return c;}public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ssreturn date2Str(c, null);}public static String date2Str(Calendar c, String format) {if (c == null) {return null;}return date2Str(c.getTime(), format);}public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ssreturn date2Str(d, null);}public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ssif (d == null) {return null;}if (format == null || format.length() == 0) {format = FORMAT;}SimpleDateFormat sdf = new SimpleDateFormat(format);String s = sdf.format(d);return s;}public static String getCurDateStr() {Calendar c = Calendar.getInstance();c.setTime(new Date());return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"+ c.get(Calendar.DAY_OF_MONTH) + "-"+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)+ ":" + c.get(Calendar.SECOND);}/** * 获得当前日期的字符串格式 * * @param format * @return */public static String getCurDateStr(String format) {Calendar c = Calendar.getInstance();return date2Str(c, format);}// 格式到秒public static String getMillon(long time) {return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);}// 格式到天public static String getDay(long time) {return new SimpleDateFormat("yyyy-MM-dd").format(time);}// 格式到毫秒public static String getSMillon(long time) {return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);}}
0 0
- 日期的操作类
- java的日期操作类
- java日期类的操作
- 使用java8新版的日期类操作日期相关
- 一个简单的日期操作类
- java之中操作日期的类
- 工具类--JAVA关于日期的操作
- 日期类的一些基本操作
- java日期类API的操作
- mysql 日期的操作
- 时间日期的操作
- 日期时间的操作
- js日期的操作
- SQLite的日期操作
- 日期的操作
- 关于日期的操作
- 数据库的日期操作
- SQL 的日期操作
- 软件危机学习报告
- 黑马程序员——JAVA之final所定义的
- 什么是重构?为什么要重构?如何重构
- 项目开发规范,数据库设计规范
- html字符转义
- 日期的操作类
- java线程的生命周期
- 如果你喜欢上了一个程序员小伙,献给所有的程序员女友
- InnoDB体系架构简介
- 关于VC中 error C2871: 'std' : does not exist or is not a namespace
- 集合栈计算机
- SUSE HA的安装
- 计数排序
- Unity中各种常用文件夹的用处