• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java日期格式之间的相互转换代码

JAVA相关 水墨上仙 2898次浏览

Java日期格式之间的相互转换代码

package com.hxhk.cc.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.lowagie.text.pdf.codec.postscript.ParseException;
public class DateUtil {
	/**
	 * @param args
	 * @throws java.text.ParseException 
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws ParseException, java.text.ParseException {
		DateUtil du = new DateUtil();
		//String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
		long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
		long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
		String date = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
		System.out.println(time);
		System.out.println(time1);
		System.out.println(date);
		
	}
    // date类型转换为String类型
	  // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
	  // data Date类型的时间
	  public static String dateToString(Date data, String formatType) {
	  return new SimpleDateFormat(formatType).format(data);
	  }
	 
	  // long类型转换为String类型
	  // currentTime要转换的long类型的时间
	  // formatType要转换的string类型的时间格式
	  public static String longToString(long currentTime, String formatType)
	  throws ParseException, java.text.ParseException {
	  Date date = longToDate(currentTime, formatType); // long类型转成Date类型
	  String strTime = dateToString(date, formatType); // date类型转成String
	  return strTime;
	  }
	 
	  // string类型转换为date类型
	  // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
	  // HH时mm分ss秒,
	  // strTime的时间格式必须要与formatType的时间格式相同
	  public static Date stringToDate(String strTime, String formatType)
	  throws ParseException, java.text.ParseException {
	  SimpleDateFormat formatter = new SimpleDateFormat(formatType);
	  Date date = null;
	  date = formatter.parse(strTime);
	  return date;
	  }
	 
	  // long转换为Date类型
	  // currentTime要转换的long类型的时间
	  // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
	  public static Date longToDate(long currentTime, String formatType)
	  throws ParseException, java.text.ParseException {
	  Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
	  String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
	  Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
	  return date;
	  }
	 
	  // string类型转换为long类型
	  // strTime要转换的String类型的时间
	  // formatType时间格式
	  // strTime的时间格式和formatType的时间格式必须相同
	  public static long stringToLong(String strTime, String formatType)
	  throws ParseException, java.text.ParseException {
	  Date date = stringToDate(strTime, formatType); // String类型转成date类型
	  if (date == null) {
	  return 0;
	  } else {
	  long currentTime = dateToLong(date); // date类型转成long类型
	  return currentTime;
	  }
	  }
	 
	  // date类型转换为long类型
	  // date要转换的date类型的时间
	  public static long dateToLong(Date date) {
	  return date.getTime();
	  }
	  public static String numToDate(int number,String formatType){
		  Date date = new Date(number);
		  SimpleDateFormat sdf = new SimpleDateFormat(formatType);
		  return sdf.format(date);
	  }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java日期格式之间的相互转换代码
喜欢 (0)
加载中……