⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datedeal.java

📁 局域网飞鸽传输
💻 JAVA
字号:
/**
  * @(#)tools.DateDeal.java  2008-9-2  
  * Copy Right Information	: Tarena
  * Project					: JavaQQ
  * JDK version used		: jdk1.6.4
  * Comments				: 日期处理类。
  * Version					: 1.0
  * Sr	Date		Modified By		Why & What is modified
  * 1.	2008-9-2 	小猪     		新建
  **/
package tools;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

 /**
 * 日期处理类。
 * 2008-9-2
 * @author		达内科技[Tarena Training Group]
 * @version	1.0
 * @since		JDK1.6(建议) 
 */
public class DateDeal {

	/**
	 * 将当前日期返回"yyyy-MM-dd"的字符串表现形式。
	 * @return 返回当前日期的"yyyy-MM-dd"的字符串表现形式。
	 */
	public static String getCurrentDate(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(new Date());
	}
	
	/**
	 * 将当前日期返回"yyyy年MM月dd日 HH:mm:ss"的字符串表现形式。
	 * @return 返回当前日期的"yyyy年MM月dd日 HH:mm:ss"的字符串表现形式。
	 */
	public static String getCurrentTime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
		return df.format(new Date());
	}
	
	/**
	 * 将Date的日期返回"yyyy-MM-dd HH:mm:ss"的字符串表现形式。
	 * @param date Date对象。
	 * @return 返回"yyyy-MM-dd HH:mm:ss"的字符串表现形式。
	 */
	public static String getDate(Date date){
		SimpleDateFormat  df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	/**
	 * 根据当前日期返回不同形式的字符串形式。
	 * @param date Date对象。
	 * @return 如果与当前时间所在年月日相同,则返回"HH:mm:ss"形式,否则返回"yyyy-MM-dd HH:mm:ss"。
	 */
	public static String getDate2(Date date){
		GregorianCalendar g1 = new GregorianCalendar();
		g1.setTime(date);
		GregorianCalendar g2 = new GregorianCalendar();
		g2.setTime(new Date());
		SimpleDateFormat  df = null;
		if(g1.get(Calendar.YEAR)==g2.get(Calendar.YEAR) && g1.get(Calendar.MONTH)==g2.get(Calendar.MONTH) && g1.get(Calendar.DAY_OF_MONTH)==g2.get(Calendar.DAY_OF_MONTH)) 
			df = new SimpleDateFormat("HH:mm:ss");
		else
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	/**
	 * 根据当前日期返回类似:2008-10-09 星期四 13:53:51 格式的日期。
	 * @param date Data对象。
	 * @return 返回2008-10-09 星期四 13:53:51形式的日期。
	 */
	public static String getAllDate(Date date){
		Vector<String> v = new Vector<String>();
		v.add("星期日");v.add("星期一");v.add("星期二");v.add("星期三");v.add("星期四");v.add("星期五");v.add("星期六");
		String s = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		s += df.format(date);
		GregorianCalendar g1 = new GregorianCalendar();
		g1.setTime(date);
		int n = g1.get(GregorianCalendar.DAY_OF_WEEK) - 1;
		s += " "+v.get(n) +" ";
		df = new SimpleDateFormat("HH:mm:ss");
		s += df.format(date);
		return s;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -