您的位置:

java当天上一天的日期(java这一天是这一年的第几天)

本文目录一览:

java怎样获得当前时间的前几天

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));

如何利用java得到当前的时间和前一天的时间

当前时间 Date(System.currentTimeMillis())

前一天:Calendar cd = Calendar.getInstance();

cd.setTime(Date(System.currentTimeMillis()));

cd.add(Calendar.DATE, -1);// 减少一天

return cd.getTime();

java如何得到上周一和上周日的日期

/**

* 获取上周五时间

*/

public Date lastFirday() {

//作用防止周日得到本周日期

Calendar calendar = Calendar.getInstance();

while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {

calendar.add(Calendar.DAY_OF_WEEK, -1);

}

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

int offset = 7 - dayOfWeek;

calendar.add(Calendar.DATE, offset - 9);

return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);//这是从上周日开始数的到本周五为6

}

/**

* 获取上周一时间

*/

public Date lastMonday() {

Calendar calendar = Calendar.getInstance();

while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {

calendar.add(Calendar.DAY_OF_WEEK, -1);

}

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

int offset = 1 - dayOfWeek;

calendar.add(Calendar.DATE, offset - 7);

return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);

}

Java 获取前一天起始和结束时间!

public static void main(String[] args){

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-dd"); //设置时间格式

String defaultStartDate = sdf.format(dBefore); //格式化前一天

defaultStartDate = defaultStartDate+" 00:00:00";

String defaultEndDate = defaultStartDate.substring(0,10)+" 23:59:59";

System.out.println("前一天的起始时间是:" + defaultStartDate);

System.out.println("前一天的结束时间是:" + defaultEndDate);

}

java获取当前时间的前一天

代码如下:

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

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-dd HH:mm:ss"); //设置时间格式

String defaultStartDate = sdf.format(dBefore);    //格式化前一天

String defaultEndDate = sdf.format(dNow); //格式化当前时间

System.out.println("前一天的时间是:" + defaultStartDate);

System.out.println("生成的时间是:" + defaultEndDate);

java简介:

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等