日期:2014-05-20  浏览次数:20937 次

如何得到一个日期的前一天日期,前一个月的日前,前一年的日期?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  // TODO add your handling code here:
  JFormattedTextField jTextDate = getTextField(dateJSpinner);
  String strDate = jTextDate.getText();
   
  System.out.println(strDate);
  }
  public JFormattedTextField getTextField(JSpinner spinner) {
  JComponent editor = spinner.getEditor();
  if (editor instanceof JSpinner.DefaultEditor) {
  return ((JSpinner.DefaultEditor)editor).getTextField();
  } else {
  System.err.println("Unexpected editor type: "
  + spinner.getEditor().getClass()
  + " isn't a descendant of DefaultEditor");
  return null;
  }
  }
//得到spinner的日期在jButton1ActionPerformed中,想得到这个日期(格式0000-00-00)的的前一天日期,前一个月的日前,前一年的日期?是按String 的运算能得到吗?还是别的?
 

------解决方案--------------------
JODA time 你网上搜索一下吧.很好用的包
------解决方案--------------------
获得一个Calendar实例,在利用add方法。

类似:
cal.add( GregorianCalendar.MONTH , -2 );
cal.add( GregorianCalendar.YEAR , 1 );

想减去多少天都可以。
------解决方案--------------------
Calendar cal = Calendar.getInstance();
Calendar calClone = (Calendar) cal.clone();
calClone.setTime(new Date());
calClone.set(Calendar.DAY_OF_YEAR, calClone.get(Calendar.DAY_OF_YEAR) - 1);
System.out.println(calClone.getTime());

calClone = (Calendar) cal.clone();
calClone.setTime(new Date());
calClone.set(Calendar.MONTH, calClone.get(Calendar.MONTH) - 1);
System.out.println(calClone.getTime());

calClone = (Calendar) cal.clone();
calClone.setTime(new Date());
calClone.set(Calendar.YEAR, calClone.get(Calendar.YEAR) - 1);
System.out.println(calClone.getTime());
------解决方案--------------------
好像写多了
Calendar cal = Calendar.getInstance();
Calendar calClone = (Calendar) cal.clone();
calClone.set(Calendar.DAY_OF_YEAR, calClone.get(Calendar.DAY_OF_YEAR) - 1);
System.out.println(calClone.getTime());

calClone = (Calendar) cal.clone();
calClone.set(Calendar.MONTH, calClone.get(Calendar.MONTH) - 1);
System.out.println(calClone.getTime());

calClone = (Calendar) cal.clone();
calClone.set(Calendar.YEAR, calClone.get(Calendar.YEAR) - 1);
System.out.println(calClone.getTime());

------解决方案--------------------
cal.add( GregorianCalendar.MONTH , -2 ); 
cal.add( GregorianCalendar.YEAR , 1 );
------解决方案--------------------
看看这个:
http://blog.csdn.net/dazhen520/archive/2007/05/29/1629914.aspx

http://blog.csdn.net/dazhen520/archive/2007/05/29/1629220.aspx
------解决方案--------------------
一定要gregoriencalender,
juliancalendar 没有润日
Calendar calendar = new GregorianCalendar(pdt);
------解决方案--------------------
搞日期用calendar