当前位置: 技术问答>java相关
怎么得到3天前的日期?
来源: 互联网 发布时间:2015-02-05
本文导语: 如今天是2001-09-20 14.01.22 怎么能得到2001-09-17 14.01.22? | import java.util.*; import java.text.*; public class ThreeDaysAgo { public static void main(String args[]) { GregorianCalendar calendar = new GregorianCalendar()...
如今天是2001-09-20 14.01.22
怎么能得到2001-09-17 14.01.22?
怎么能得到2001-09-17 14.01.22?
|
import java.util.*;
import java.text.*;
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
}
}
import java.text.*;
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
}
}
|
大概
Calendar.add(Calendar.DAY,-3);
System.out.pritln(Calendar.getTime());
Calendar.add(Calendar.DAY,-3);
System.out.pritln(Calendar.getTime());
|
你要自己写!
存在有效性的问题!
3.1的前三天是几号?要通过你自己的算法判断!
存在有效性的问题!
3.1的前三天是几号?要通过你自己的算法判断!