java中常用的时间工具类_java中常用的时间工具类整理

package com.help; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /**  * 时间处理工具类  *   * */ public final class MyDate { /** * 生成当前时间 格式为 xxxx/xx/xx xx:xx:xx * */ public static String getNowTime1(){ Date now=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String time=format.format(now); return time; } /** * 生成当前时间 格式为 xxxx年xx月xx日  xx时xx分xx秒 * */ public static String getNowTime2(){ Date now=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日  HH时mm分ss秒"); String time=format.format(now); return time; } /** * 生成当前日期 格式为 xxxx年xx月xx日 * */ public static String getNowDate1(){ Calendar c=Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH); int date=c.get(Calendar.DATE); String str=year+"年"+month+"月"+date+"日"; return str; } public static String getNowDate2(){ Calendar c=Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH); int date=c.get(Calendar.DATE); String str=year+"/"+month+"/"+date; return str; } }

package com.bdvcd.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; /** * 文件名:DateUtils.java 日期处理相关工具类 * 版本信息:V1.0 * 日期:2013-03-11 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class DateUtils { /**定义常量**/ public static final String DATE_JFP_STR="yyyyMM"; public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_SMALL_STR = "yyyy-MM-dd"; public static final String DATE_KEY_STR = "yyMMddHHmmss"; /** * 使用预设格式提取字符串日期 * @param strDate 日期字符串 * @return */ public static Date parse(String strDate) { return parse(strDate,DATE_FULL_STR); } /** * 使用用户格式提取字符串日期 * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Date parse(String strDate, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { return df.parse(strDate); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 两个时间比较 * @param date * @return */ public static int compareDateWithNow(Date date1){ Date date2 = new Date(); int rnum =date1.compareTo(date2); return rnum; } /** * 两个时间比较(时间戳比较) * @param date * @return */ public static int compareDateWithNow(long date1){ long date2 = dateToUnixTimestamp(); if(date1>date2){ return 1; }else if(date1<date2){ return -1; }else{ return 0; } } /** * 获取系统当前时间 * @return */ public static String getNowTime() { SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR); return df.format(new Date()); } /** * 获取系统当前时间 * @return */ public static String getNowTime(String type) { SimpleDateFormat df = new SimpleDateFormat(type); return df.format(new Date()); } /** * 获取系统当前计费期 * @return */ public static String getJFPTime() { SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR); return df.format(new Date()); } /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd HH:mm:ss * @return long 时间戳 */ public static long dateToUnixTimestamp(String date) { long timestamp = 0; try { timestamp = new SimpleDateFormat(DATE_FULL_STR).parse(date).getTime(); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd * @return long 时间戳 */ public static long dateToUnixTimestamp(String date, String dateFormat) { long timestamp = 0; try { timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime(); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } /** * 将当前日期转换成Unix时间戳 * @return long 时间戳 */ public static long dateToUnixTimestamp() { long timestamp = new Date().getTime(); return timestamp; } /** * 将Unix时间戳转换成日期 * @param long timestamp 时间戳 * @return String 日期字符串 */ public static String unixTimestampToDate(long timestamp) { SimpleDateFormat sd = new SimpleDateFormat(DATE_FULL_STR); sd.setTimeZone(TimeZone.getTimeZone("GMT+8")); return sd.format(new Date(timestamp)); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值