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

关于获取日期
怎么把获得的当前日期,减去一天,获得昨天的日期?

------解决方案--------------------
一个显示系统日期的小程序: 

import java.awt.*; 
import java.Applet.*; 
import java.util.*; 

public class showtimeApplet extends Applet{ 
Calendar calCurrent=Calendar.getInstance(); 
TextArea txaDate=new TextArea(5,20); 
int intDay=calCurrent.get(Calendar.DATE); 
int intMonth=calCurrent.get(Calendar.MONTH)+1; 
int intYear=calCurrent.get(Calendar.YEAR); 

public void init(){ 
add(txaDate); 
txaDate.append(intYear+"-"+intMonth+"-"+intDay); 



2.java显示当前日期时间


import java.util.*; 
public class Calendar 

public static void main(String[] args) 

GregorianCalendar calendar=new GregorianCalendar(); 
//取出当前的年,月,日 
int year=calendar.get(calendar.YEAR); 

//月的数值加1,使之变成习惯的月份大小(1~12月) 
int month=calendar.get(calendar.MONTH)+1; 
int today=calendar.get(calendar.DAY_OF_MONTH); 

//输出当前的年,月,日及星期的格式样本 
System.out.println("Today is "+year+"/"+month+"/"+today+"!"); 
System.out.println(); 
System.out.println("Sun Mon Tue Wed Thu Fri Sat"); //中间为两个空格 

//设置从当前月的1号开始显示日历并查看他是一个星期的第几天 
calendar.set(calendar.DAY_OF_MONTH,1); 
int weekday=calendar.get(calendar.DAY_OF_WEEK); 

//使1号对齐相应的星期 
for(int i=calendar.SUNDAY;i System.out.print(" "); //中间为5个空格 
int day,Month; 
//用do-while循环来实现日历的输出 
do{ 
day=calendar.get(calendar.DAY_OF_MONTH); 
System.out.print(day); 

/*日期是今天则多输出一个“*”用于标识,日期不是今天就判断是否小于10, 
输出一定的空格。这主要是为了使格式对齐。 */ 
if(day==today) 

System.out.print("*"); 
if(day<10)System.out.print(" "); 

else if(day<10)System.out.print(" "); 
else System.out.print(" "); 
weekday=calendar.get(calendar.DAY_OF_WEEK); 

//根据格式要求,星期六要换行.如果不换行,则输出一定的空格 
if(weekday==calendar.SATURDAY)System.out.println(); 
else System.out.print(" "); 

calendar.add(calendar.DAY_OF_WEEK,1); 
Month=calendar.get(calendar.MONTH)+1; 

while(Month==month); //如果Month超过当前月,则退出循环, 
System.out.println(); 
System.out.println(); 

}
------解决方案--------------------
Java code

public class App{
    
    public static void main(String[] args) throws InterruptedException {
        
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        
        Date date = new Date();
        System.out.println(format.format(date));
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        
        Date yesterday = calendar.getTime();
        System.out.println(format.format(yesterday));
        
        
    }
    
    
}

------解决方案--------------------
Calendar就可以
Java code
Calendar c = Calendar.getInstance(); //获取Calendar实例
c.setTime(new Date()); //设置为今天日期
c.add(Calendar.DATE, -1); //获取的昨天的日期
System.out.printf("%tF\n", c.getTime());