关于Calendar工具类的使用

设置、获取时间里的某一个选项时,使用Calendat

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    //通过Calendar 操作时间
    //得到日历对象
    //日历对象是单例模式,只有一个对象
    Calendar calendar = Calendar.getInstance(Locale.CHINA);

    System.out.println(calendar.getTimeInMillis());
    //设置日历显示的时间
    calendar.set(2000, 3, 15, 9, 14);
    System.out.println(calendar.getTimeInMillis());
    //练习:9.10到目前为止多少天
    long oneDay = 1*1000*60*60*24;
    calendar.set(2017, 10,16,10,16);
    long now = calendar.getTimeInMillis();
    calendar.set(2017, 9,10,1,1);
    long old = calendar.getTimeInMillis();
    int day = (int) ((now-old)/oneDay);
    System.out.println(day);
    //获取系统当前时间
    System.out.println(calendar.getTime());
    //获取系统时区
    System.out.println(calendar.getTimeZone());
    //可以 格式化时间
    Calendar c = new GregorianCalendar();
    //设置时区并输出
    c.setTimeZone(TimeZone.getTimeZone("CST"));
    System.out.println(c.getTimeZone());
    //在new出的对象中格式化时间
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern("yyyy/MM/dd HH:mm:ss");
    String strTime = sdf.format(c.getTime());
    System.out.println(strTime);

    //Date Calender 怎么选择?
    //获取系统时间 Date(格式化时间)
    //设置时间 Calendar 获取时间里的某个选项
}
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、付费专栏及课程。

余额充值