java怎样获得当前时间的前几天
发布网友
发布时间:2022-04-23 22:13
我来回答
共4个回答
热心网友
时间:2023-09-12 22:57
1.java中关于获取时间的所有方式如下,
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
dBefore = calendar.getTime(); //得到前一天的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置时间格式
String defaultStartDate = sdf.format(dBefore); //格式化前一天
String defaultEndDate = sdf.format(dNow); //格式化当前时间
System.out.println("前一天的时间是:" + defaultStartDate);
System.out.println("生成的时间是:" + defaultEndDate);
2.如上方式能得到当前时间的前一天时间,同理,可以得到前一天的前一天,那么,前几天的时间都可以这样获得,如果觉得方案还是不可行,可以使用第三方的日历组件来获得更加准确的全年三百六十五天的时间
另有:
Calendar 与 Date 的转换:
Calendar calendar = Calendar.getInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendar.getTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// 先获得一个实例,然后设置 Date 对象
calendar.setTime(date);
月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8.
calendar.set(Calendar.MONTH, 7);
计算2000-01-01是星期几
calendar.get(calendar.DAY_OF_WEEK )
3.实例代码测试如下
Java代码
import Java.util.*;
public class ShowDate {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();
calendar.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
热心网友
时间:2023-09-12 22:58
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class $ {
public static void main(String[] args) {
Format f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar c = Calendar.getInstance();
System.out.println("当前时间:" + f.format(c.getTime()));
// 前一天
c = test(c, -1);
System.out.println(" 前一天:" + f.format(c.getTime()));
// 后两天
test(c, 2);
System.out.println(" 后两天:" + f.format(c.getTime()));
}
private static Calendar test(Calendar c, int day) {
c.add(Calendar.DATE, day);
return c;
}
}
热心网友
时间:2023-09-12 22:58
Calendar类的add方法,第二个引数用负数就行,。
热心网友
时间:2023-09-12 22:59
Date d = new Date();
d是当前时间