java 获取当前时间,将天数减一天,返回Date.

java 获取当前时间,将天数减一天,返回Date.,第1张

具体如下:

javautilCalendar

voidsetTime(Date date)

Sets this Calendar's time with the given Date设置日期时间

abstract  void add(int field,  int amount)

Adds or subtracts the specified amount of time to the given calendar field,

based on the calendar's rules

参数1是  CalendarDAY_OF_MONTH

参数2是天数,如果是负数,则为减

DategetTime()

Returns a Date object representing thisCalendar's time value (millisecond offset from the Epoch")得到时间

扩展资料

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

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

(参考资料 百度百科 Java)

Calendar

c

=

CalendargetInstance();//可以对每个时间域单独修改

//cset(year,month,date);如果你随意输入时间

//Date

date

=

cgetTime();得到date

//如果格式还要求

就用SimpleDateFormat

sf=new

SimpleDateFormat("YYYY-MM-DD");

int

year

=

cget(CalendarYEAR);

int

month

=

cget(CalendarMONTH);

int

date

=

cget(CalendarDATE);

int

hour

=

cget(CalendarHOUR_OF_DAY);

int

minute

=

cget(CalendarMINUTE);

int

second

=

cget(CalendarSECOND);

Systemoutprintln("当前时间"+year

+

"/"

+

month

+

"/"

+

date

+

"

"

+hour

+

":"

+minute

+

":"

+

second);

cset(year,

month,

date+1);

year

=

cget(CalendarYEAR);

month

=

cget(CalendarMONTH);

date

=

cget(CalendarDATE);

hour

=

cget(CalendarHOUR_OF_DAY);

minute

=

cget(CalendarMINUTE);

second

=

cget(CalendarSECOND);

Systemoutprintln("当前时间"+year

+

"/"

+

month

+

"/"

+

date

+

"

"

+hour

+

":"

+minute

+

":"

+

second);

//这是我以前联系着玩用的

你可以看看

public static void main(String[] args) {

Calendar calendar = CalendargetInstance(LocalegetDefault());

Systemoutprintln(calendarget(CalendarYEAR));

Systemoutprintln(calendarget(CalendarMONTH)+1);

Systemoutprintln(calendarget(CalendarDATE));

}

3行代码分别用于获取当前时间的年、月、日,获取月份的时候需要+1,因为月份取的索引值,从0-11

String str1 = "2014-11-20";

DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd");

Date date1 = fmtDateTimeparse(str1toString());

Calendar calendar1 = CalendargetInstance();

calendar1setTime(date1);

Calendar  today = CalendargetInstance();

double days = (todaygetTimeInMillis() - calendar1getTimeInMillis()) / (1000  60  60  24);

import javatextSimpleDateFormat;import javautilDate;

public class Timedemo {

public static void main(String[] args)

{

long time=SystemcurrentTimeMillis();

Date date=new Date(time);

String mat="yyyy-MM-dd";

String ma="yyyyMMdd";

SimpleDateFormat format=new SimpleDateFormat(mat);

SimpleDateFormat forma=new SimpleDateFormat(ma);

String nowdate=formatformat(date);

String nwdate=formaformat(date);

int x=IntegerparseInt(nwdate);

Systemoutprintln(nowdate);

Systemoutprintln(nwdate);

Systemoutprintln(x);

}

}

经过测试满足以上条件 希望对你有帮助

1java中关于获取时间的所有方式如下,

Date dNow = new Date();   //当前时间

Date dBefore = new Date();

Calendar calendar = CalendargetInstance(); //得到日历

calendarsetTime(dNow);//把当前时间赋给日历

calendaradd(CalendarDAY_OF_MONTH, -1);  //设置为前一天

dBefore = calendargetTime();   //得到前一天的时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置时间格式

String defaultStartDate = sdfformat(dBefore);    //格式化前一天

String defaultEndDate = sdfformat(dNow); //格式化当前时间

Systemoutprintln("前一天的时间是:" + defaultStartDate);

Systemoutprintln("生成的时间是:" + defaultEndDate);

2如上方式能得到当前时间的前一天时间,同理,可以得到前一天的前一天,那么,前几天的时间都可以这样获得,如果觉得方案还是不可行,可以使用第三方的日历组件来获得更加准确的全年三百六十五天的时间

另有:

Calendar 与 Date 的转换:

Calendar calendar = CalendargetInstance();

// 从一个 Calendar 对象中获取 Date 对象

Date date = calendargetTime();

// 将 Date 对象反应到一个 Calendar 对象中,

// 先获得一个实例,然后设置 Date 对象

calendarsetTime(date);

月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8

calendarset(CalendarMONTH, 7);

计算2000-01-01是星期几

calendarget(calendarDAY_OF_WEEK )

3实例代码测试如下

 Java代码

import Javautil;

public class ShowDate {

public static void main(String[] args) {

Calendar calendar = new GregorianCalendar();

Date trialTime = new Date();

calendarsetTime(trialTime);

// print out a bunch of interesting things

Systemoutprintln("ERA: " + calendarget(CalendarERA));

Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));

Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));

Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));

Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));

Systemoutprintln("DATE: " + calendarget(CalendarDATE));

Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));

Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));

Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));

Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));

Systemoutprintln("AM_PM: " + calendarget(CalendarAM_PM));

Systemoutprintln("HOUR: " + calendarget(CalendarHOUR));

Systemoutprintln("HOUR_OF_DAY: " + calendarget(CalendarHOUR_OF_DAY));

Systemoutprintln("MINUTE: " + calendarget(CalendarMINUTE));

Systemoutprintln("SECOND: " + calendarget(CalendarSECOND));

Systemoutprintln("MILLISECOND: " + calendarget(CalendarMILLISECOND));

Systemoutprintln("ZONE_OFFSET: " + (calendarget(CalendarZONE_OFFSET)/(60601000)));

Systemoutprintln("DST_OFFSET: " + (calendarget(CalendarDST_OFFSET)/(60601000)));

Systemoutprintln("Current Time, with hour reset to 3");

calendarclear(CalendarHOUR_OF_DAY); // so doesn't override

calendarset(CalendarHOUR, 3);

Systemoutprintln("ERA: " + calendarget(CalendarERA));

Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));

Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));

Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));

Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));

Systemoutprintln("DATE: " + calendarget(CalendarDATE));

Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));

Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));

Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));

Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));

以上就是关于java 获取当前时间,将天数减一天,返回Date.全部的内容,包括:java 获取当前时间,将天数减一天,返回Date.、用java怎么获取任意一天的下一天的时间的方法、java获取当前时间几天/月/年前的时间方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9495378.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存