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

📄 datetime.java

📁 一个用java开发的具有搜索功能的图书管理系统
💻 JAVA
字号:
package library;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime {
	
	private static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat timeFormat=new SimpleDateFormat("hh:mm:ss");
	private static SimpleDateFormat dateTimeFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
	
	public static final int YEAR=32;
	public static final int MONTH=23;
	public static final int WEEK=31;
	public static final int DAY=2;
	
	public static final int HOUR=22;
	public static final int MINUTE=89;
	public static final int SECEND=8;
	
	public DateTime(){}

	private static Date getNowTime()
	{
		Calendar ctime=Calendar.getInstance();
		return ctime.getTime();
	}
	/**
	 * @return date
	 */
	public static String getDate() {
		return dateFormat.format(getNowTime());
	}

	/**
	 * @return dateTime
	 */
	public static String getDateTime() {
		return dateTimeFormat.format(getNowTime());
	}

	/**
	 * @return time
	 */
	public static String getTime() {
		return timeFormat.format(getNowTime());
	}

	
	//日期加减  格式为yyyy-MM-dd
	public static String addDate(String date,int addDate,int type) throws Exception
	{
		Calendar temp=Calendar.getInstance();
		Date d=dateFormat.parse(date);
		temp.setTime(d);
		switch(type)
		{
		case YEAR:
			temp.add(Calendar.YEAR,addDate);
			break;
		case MONTH:
			temp.add(Calendar.MONTH,addDate);
			break;
		case WEEK:
			temp.add(Calendar.DAY_OF_MONTH,addDate*7);
			break;
		case DAY:
			temp.add(Calendar.DAY_OF_MONTH,addDate);
			break;
		default:
			throw new Exception("指定类型不准确。");
		}
		return dateFormat.format(temp.getTime());
	}
	//日期相加  格式为yyyy-MM-dd			返回相差天数
	public static int dayOfMinus(String date,String minusDate) throws ParseException
	{
//		String[] fields=addDate.split("-");
		
		
		
//		int field=0;
//		
//		for(int i=0;i<fields.length;i++)
//		{
//			switch(i)
//			{
//			case 0:
//				field=Calendar.YEAR;
//				break;
//			case 1:
//				field=Calendar.MONTH;
//				break;
//			case 2:
//				field=Calendar.DAY_OF_MONTH;
//				break;
//			default:
//				throw new Exception("日期格式不准确。");
//			}
//			if(isMinus==false)
//			{
//				temp.add(field,Integer.parseInt(fields[i]));
//			}
//			else
//			{
//				temp.add(field,-Integer.parseInt(fields[i]));
//			}
//		}
//		
		
//		Calendar tempAdd=Calendar.getInstance();
		Date d=dateFormat.parse(date);
		Date dMinus=dateFormat.parse(minusDate);
//		tempAdd.setTime(d);
		int dayOffset=(int)((d.getTime()-dMinus.getTime())/1000/24/60/60);
		return dayOffset;
	}
	
	//时间加减  格式为hh:mm:ss
	public static String addTime(String date,int addDate,int type) throws Exception
	{
		Calendar temp=Calendar.getInstance();
		Date d=timeFormat.parse(date);
		temp.setTime(d);
		switch(type)
		{
		case HOUR:
			temp.add(Calendar.HOUR,addDate);
			break;
		case MINUTE:
			temp.add(Calendar.MINUTE,addDate);
			break;
		case SECEND:
			temp.add(Calendar.SECOND,addDate);
			break;
		default:
			throw new Exception("指定类型不准确。");
		}
		return timeFormat.format(temp.getTime());
	}
	//日期时间加减  格式为yyyy-MM-dd hh:mm:ss
	public static String addDateTime(String date,int addDate,int type) throws Exception
	{
		Calendar temp=Calendar.getInstance();
		Date d=dateTimeFormat.parse(date);
		temp.setTime(d);
		switch(type)
		{
		case YEAR:
			temp.add(Calendar.YEAR,addDate);
			break;
		case MONTH:
			temp.add(Calendar.MONTH,addDate);
			break;
		case WEEK:
			temp.add(Calendar.DAY_OF_MONTH,addDate*7);
			break;
		case DAY:
			temp.add(Calendar.DAY_OF_MONTH,addDate);
			break;
		case HOUR:
			temp.add(Calendar.HOUR,addDate);
			break;
		case MINUTE:
			temp.add(Calendar.MINUTE,addDate);
			break;
		case SECEND:
			temp.add(Calendar.SECOND,addDate);
			break;
		default:
			throw new Exception("指定类型不准确。");
		}
		return dateTimeFormat.format(temp.getTime());
	}
	public static int getYear()
	{
		Calendar temp=Calendar.getInstance();
		return temp.get(Calendar.YEAR);
	}
}

⌨️ 快捷键说明

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