`
zjx2388
  • 浏览: 1337656 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA中关于日期的处理(未完,待续)

    博客分类:
  • J2SE
阅读更多
写道
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
String today = sdf.format(date);
long time = System.currentTimeMillis();
System.out.println("today:"+today+"\ntime:"+time);

 

Calendar calendar = Calendar.getInstance(); 
calendar.add(Calendar.DATE, -1);    //得到前一天 
calendar.add(Calendar.MONTH, -1);    //得到前一个月 
int year = calendar.get(Calendar.YEAR); 
int month = calendar.get(Calendar.MONTH)+1; 
注意月份加一 


/**  
    * 判断当前日期是星期几<br>  
    * <br>  
    * @param pTime 修要判断的时间<br>  
    * @return dayForWeek 判断结果<br>  
    * @Exception 发生异常<br>  
    */  
public static int dayForWeek(String pTime) throws Exception {   
format = new SimpleDateFormat("yyyy-MM-dd");   
Calendar c = Calendar.getInstance();   
c.setTime(format.parse(pTime));   
int dayForWeek = 0;   
if(c.get(Calendar.DAY_OF_WEEK) == 1){   
  dayForWeek = 7;   
}else{   
  dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;   
}   
return dayForWeek;   
}  

package test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * JAVA将(java.util.)Date减一天
 * 
 * @author Zhou-Jingxian
 *
 */
public class TestDate {

	public static void main(String args[]) {
		Date date = new Date();
		String before = changeDateForBefore(date);
		System.out.println("before : "+ before);
	}

	public static String changeDateForBefore(Date dates) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String strDate = sdf.format(dates);

		int intYear = Integer.parseInt(strDate.substring(0, 4));
		int intMonth = Integer.parseInt(strDate.substring(4, 6));
		int intDate = Integer.parseInt(strDate.substring(6, 8));

		Calendar cal = Calendar.getInstance();
		cal.set(intYear, intMonth - 1, intDate);
		cal.add(Calendar.DATE, -1);

		Date date = cal.getTime();
		
		return sdf.format(date);
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics