java日期操作util工具包

package jdbc.util;

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

/**
 * 
 * @ClassName:  DateUtil   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 清之羽 
 * @date:   2018年8月15日 上午8:38:34   
 * 用于使对日期字符串对象的一些简单操作    
 * @Copyright: 2018 All rights reserved.
 */
public class DateUtil 
{
	/**
	 * 将含年月日时分秒的字符串日期对象转变为Date类型的对象,格式必须为yyyy年MM月dd日
	 * @param yyyy年MM月dd日HH时mm分ss秒
	 * @return
	 * @throws ParseException
	 */
	public Date getDate(String yyyy年MM月dd日HH时mm分ss秒) throws ParseException
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH时:mm分:ss秒");
		Date date=dateFormat.parse(yyyy年MM月dd日HH时mm分ss秒);
		return date;
	}
	/**
	 * 将含年月日的字符串日期对象转变为Date类型的对象
	 * @param yyyy年MM月dd日
	 * @return
	 * @throws ParseException
	 */
	public Date getDate1(String yyyy年MM月dd日) throws ParseException
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日");
		Date date=dateFormat.parse(yyyy年MM月dd日);
		return date;
	}
	/**
	 * 
	 * @param yyyy_MM_dd
	 * @return
	 * @throws ParseException
	 */
	public Date getDate2(String yyyy_MM_dd) throws ParseException
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date date=dateFormat.parse(yyyy_MM_dd);
		return date;
	}
//	2018-8-15 9:45:59->date
	public Date getDate3(String yyyy_MM_dd_HH_mm_ss) throws ParseException
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=dateFormat.parse(yyyy_MM_dd_HH_mm_ss);
		return date;
	}
	
	
	
	
	/**
	 * date->String
	 * @param date
	 * @return	yyyy年MM月dd日  返回字符串时间戳
	 */
	public String getDateString(Date date)
	{
		String strDate;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
		strDate=simpleDateFormat.format(date);
		return strDate;
	}
	/**
	 * Date->String
	 * @param date
	 * @return	yyyy年MM月dd日 HH时mm分ss秒
	 */
	public String getDateString1(Date date)
	{
		String strDate;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		strDate=simpleDateFormat.format(date);
		return strDate;
	}
	/**
	 * Date->String
	 * @param date
	 * @return	yyyy-MM-dd 
	 */
	public String getDateString3(Date date)
	{
		String strDate;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		strDate=simpleDateFormat.format(date);
		return strDate;
	}
	/**
	 * Date->String
	 * @param date
	 * @return	yyyy-MM-dd HH:mm:ss
	 */
	public String getDateString4(Date date)
	{
		String strDate;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		strDate=simpleDateFormat.format(date);
		return strDate;
	}
	
	/**
	 * 
	 * @param d1 前一个日期对象
	 * @param d2 后一个日期对象
	 * @return 获取两个日期之间的天数差
	 */
	public int getDifferenceDay(Date d1,Date d2)
	{
		long longTime1=d1.getTime();
		long longTime2=d2.getTime();
		long deferenceTime=longTime2-longTime1;
		int day=(int) (deferenceTime/(24*60*60*1000));
		return day;
	}
//	Date->Calendar;
	public Calendar getDateFormatCalendar(Date date)
	{
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
//	Calnedar->Date;
	public Date getCalendarFormatDate(Calendar calendar)
	{
		Date date=calendar.getTime();
		return date;
	}
//	Date->long
	public long getDateFormatLong(Date date)
	{
		long longTime=date.getTime();
		return longTime;
	}
//	long->Date
	public Date getLongFormatDate(long longTime)
	{
		Date date=new Date(longTime);
		return date;
	}
//	calendar->long
	public long getCalendarFormatLong(Calendar calendar)
	{
		long longTime=calendar.getTimeInMillis();
		return longTime;
	}
//	long->calendar
	public Calendar getLongFormatCalendar(long longTime)
	{
		Calendar calendar=Calendar.getInstance();
		calendar.setTimeInMillis(longTime);
		return calendar;
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值