Java中Calendar使用

一些常用的操作

import java.util.Calendar;    
import java.util.Date;    

public class TestDate {
     public static void main(String[] args) {
    	 //实例化
     	Calendar calendar=Calendar.getInstance();         				   			
     	calendar.setTime(new Date());    
     	//获取当前年份、月份、日期、小时、分钟、秒
     	int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1;  
        int day = cale.get(Calendar.DATE);  
        int hour = cale.get(Calendar.HOUR_OF_DAY);  
        int minute = cale.get(Calendar.MINUTE);  
        int second = cale.get(Calendar.SECOND);  
        //Day of Week
        int dow = cale.get(Calendar.DAY_OF_WEEK);  
        //Day of Month
        int dom = cale.get(Calendar.DAY_OF_MONTH);  
        //Day of Year
        int doy = cale.get(Calendar.DAY_OF_YEAR);  
        //设置时间
        calendar.set(2020,5,1);
        //返回此Calendar以毫秒为单位的时间
        long t=calendar.getTimeInMillis();    

Calendar类方法简介:

  • static Calendar getInstance():使用默认时区和区域设置获取日历。通过该方法生成Calendar对象。
  • public void set(int year,int month,int date,int hourofday,int minute,int second):设置日历的年、月、日、时、分、秒。
  • public int get(int field):返回给定日历字段的值。所谓字段就是年、月、日等等。
  • public void setTime(Date date):使用给定的Date设置此日历的时间。Date------Calendar
  • public Date getTime():返回一个Date表示此日历的时间。Calendar-----Date
  • abstract void add(int field,int amount): 按照日历的规则,给指定字段添加或减少时间量。
  • public long getTimeInMillies():以毫秒为单位返回该日历的时间值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值