日期工具类

package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
 * 日期工具
 * @author qmhao
 *
 */
public class DateUtil {

	/**
	 * 把日期转化为英文表示方式
	 * @param dataStr 格式为"2011-12-08 17:40:01"
	 * @return 格式为"Thursday,December 08,2011 Posted :17:40 BJT(0940 GMT)" 
	 */
	public static String parseToEnglishData(String dataStr){
		//返回字符串
		String total="";
		Date date;
		try {
			date = new   SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dataStr);
			System.out.println(date.getTime());
		String p = "EEEE,MMMM dd,yyyy 'Posted' :";
		String gmPattern = "(hhmm zz)"; //格林时间的模式
		SimpleDateFormat   chSd   =   new   SimpleDateFormat( "HH:mm"); //北京时间
		SimpleDateFormat   gmtSdf   =   new   SimpleDateFormat( p ,Locale.US); //格林时间
		gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT"));//设置 DateFormat的时间区域为GMT
		total+=gmtSdf.format(date)+chSd.format(date)+" BJT";
		SimpleDateFormat   gmtSdf2   =   new   SimpleDateFormat( gmPattern ,Locale.US); 
		gmtSdf2.setTimeZone(TimeZone.getTimeZone("GMT"));//设置 DateFormat的时间区域为GMT
		total+=gmtSdf2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return total ;	
	}
	
	/**
	 *  把日期转化为英文表示方式
	 * @param datatime 格式为 把日期转化为英文表示方式
	 * @return 格式为"Thursday,December 08,2011 Posted :17:40 BJT(0940 GMT)" 
	 */
	public static String parseToEnglishData(long datatime){
		//返回字符串
		String total="";
		Date date;
		date = new  Date(datatime);
		String p = "EEEE,MMMM dd,yyyy 'Posted' :";
		String gmPattern = "(hhmm zz)"; //格林时间的模式
		SimpleDateFormat   chSd   =   new   SimpleDateFormat( "HH:mm"); //北京时间
		SimpleDateFormat   gmtSdf   =   new   SimpleDateFormat( p ,Locale.US); //格林时间
		gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT"));//设置 DateFormat的时间区域为GMT
		total+=gmtSdf.format(date)+chSd.format(date)+" BJT";
		SimpleDateFormat   gmtSdf2   =   new   SimpleDateFormat( gmPattern ,Locale.US); 
		gmtSdf2.setTimeZone(TimeZone.getTimeZone("GMT"));//设置 DateFormat的时间区域为GMT
		total+=gmtSdf2.format(date);
		
		return total ;	
	}
	
	
	/**
	 * @param args
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws ParseException {
		System.out.println(DateUtil.parseToEnglishData("2011-12-08 17:40:01"));
		System.out.println(DateUtil.parseToEnglishData(1323337201000L));
	}

}
 
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、付费专栏及课程。

余额充值