java.util.Calendar----Calendar日历类

/**
 * 
 */
package com.zhiyou.O;

import java.util.Calendar;

/**
 * @author Administrator
 *
 */
public class CalendarTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
//		 getInstance() 单例类单一实例
//		Calendar抽象类
//		抽象类不能实例化
		Calendar rightNow = Calendar.getInstance();
//		获得年月日  时分秒都可以
		System.out.println(rightNow.get(Calendar.YEAR));
//		月份定义是从0开始的 
		System.out.println(rightNow.get(Calendar.MONTH)+ 1);
		System.out.println(rightNow.get(Calendar.DAY_OF_MONTH));
//		Calendar HOUR MINITE SECOND
//		老外的星期日是1
//		星期一是2
//		星期二是3
//		以此类推
		System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));
		
//		set设置
		rightNow.set(Calendar.YEAR, 2038);
		rightNow.set(Calendar.MONTH, 2);
		rightNow.set(Calendar.DAY_OF_MONTH, 1);
//		get  获得
		int weekVale = rightNow.get(Calendar.DAY_OF_WEEK);
		System.out.println(weekVale);
		
//		封装一个方法,给定具体的日期,返回星期几?
	}

}

 封装一个方法,给定具体的日期,返回星期几?

/**
 * 
 */
package com.zhiyou.O;

import java.util.Calendar;

/**
 * @author Administrator
 *
 */
public class CalendarClassTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CalendarClassTest demo1 = new CalendarClassTest();
		demo1.print(2025, 12, 1);
	}

	public void print(int year, int month, int day) {
		Calendar rightNow = Calendar.getInstance();

		rightNow.set(Calendar.YEAR, year);
		rightNow.set(Calendar.MONTH, month);
		rightNow.set(Calendar.DAY_OF_MONTH, day);

		int weekValue = rightNow.get(Calendar.DAY_OF_WEEK);
		System.out.println(weekValue);
	}
}

 

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

余额充值