Java.util.Calendar.add()方法实例

下面的示例演示java.util.Calendar.Add()方法的用法。

package com.yiibai;
import java.util.Calendar;

public class CalendarDemo {

   public static void main(String[] args) {
   
      // create a calendar
      Calendar cal = Calendar.getInstance()
	  
      // print current date
      System.out.println("The current date is : " + cal.getTime());

      // add 20 days to the calendar
      cal.add(Calendar.DATE, 20);
      System.out.println("20 days later: " + cal.getTime());

      // subtract 2 months from the calendar
      cal.add(Calendar.MONTH, -2);
      System.out.println("2 months ago: " + cal.getTime());

      // subtract 5 year from the calendar
      cal.add(Calendar.YEAR, -5);
      System.out.println("5 years ago: " + cal.getTime());
   }
}

让我们来编译和运行上面的程序,这将产生以下结果:

The current date is : Thu Apr 26 02:13:28 EEST 2012
20 days later: Wed May 16 02:13:28 EEST 2012
2 months ago: Fri Mar 16 02:13:28 EET 2012
5 years ago: Fri Mar 16 02:13:28 EET 2007
private Chooser(java.util.Date date, String format, int startDAY){ if(startDAY > -1 && startDAY < 7) defaultStartDAY = startDAY; int dayIndex = defaultStartDAY; for(int i=0; i<7; i++){ if(dayIndex > 6) dayIndex = 0; weekLabels[i] = new WeekLabel(dayIndex, showTEXT[dayIndex]); dayIndex ++ ; } sdf = new java.text.SimpleDateFormat(format); calendar = Calendar.getInstance(); calendar.setTime(date); initCalendarPanel(); } public static Chooser getInstance(java.util.Date date, String format){ return new Chooser(date, format, defaultStartDAY); } public static Chooser getInstance(java.util.Date date){ return getInstance(date, DEFAULTFORMAT); } public static Chooser getInstance(String format){ return getInstance(new java.util.Date(), format); } public static Chooser getInstance(){ return getInstance(new java.util.Date(), DEFAULTFORMAT); } private void initCalendarPanel(){ calendarPanel = new JPanel(new java.awt.BorderLayout()); calendarPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0xAA, 0xAA, 0xAA))); calendarPanel.add(titlePanel = new TitlePanel(), java.awt.BorderLayout.NORTH); calendarPanel.add(bodyPanel = new BodyPanel(), java.awt.BorderLayout.CENTER); calendarPanel.add(footerPanel = new FooterPanel(),java.awt.BorderLayout.SOUTH); this.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { } public void ancestorRemoved(AncestorEvent event) {hidePanel();} //hide pop when move component. public void ancestorMoved(AncestorEvent event) { hidePanel(); } }); } public void register(final JComponent showComponent) { this.showDate = showComponent; showComponent.setRequestFocusEnabled(true); showComponent.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showComponent.requestFocusInWindow(); } }); this.add(showComponent, BorderLayout.CENTER); this.setPreferredSize(new Dimension(90, 25)); this.setBorder(BorderFactory.createLineBorder(Color.GRAY)); showComponent.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setCursor(new Cursor(Cursor.HAND_CURSOR)); } } public void mouseExited(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); showComponent.setForeground(Color.BLACK); } } public void mousePressed(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setForeground(hoverColor); if (isShow) { hidePanel(); } else { showPanel(showComponent); } } } public void mouseReleased(MouseEvent me) { if (showComponent.isEnabled()) { showComponent.setForeground(Color.BLACK); } } }); showComponent.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) { hidePanel(); } public void focusGained(FocusEvent e) { } }); } //hide the main panel. private void hidePanel() { if (pop != null) { isShow = false; pop.hide(); pop = null; } }逐行解释
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值