Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to get the current time of the previous week, last month, and last year

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is "how to use java to get the time of the previous week, last month, and last year". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use java to get the time of the previous week, last month, and last year".

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")

Calendar c = Calendar.getInstance ()

1. For the past seven days

C.setTime (new Date ())

C.add (Calendar.DATE,-7)

Date d = c.getTime ()

String day = format.format (d)

System.out.println ("past Seven days:" + day)

two。 In the past month

C.setTime (new Date ())

C.add (Calendar.MONTH,-1)

Date m = c.getTime ()

String mon = format.format (m)

System.out.println ("Last month:" + mon)

3. For the past three months,

C.setTime (new Date ())

C.add (Calendar.MONTH,-3)

Date m3 = c.getTime ()

String mon3 = format.format (m3)

System.out.println ("Last three months:" + mon3)

4. In the past year

C.setTime (new Date ())

C.add (Calendar.YEAR,-1)

Date y = c.getTime ()

String year = format.format (y)

System.out.println ("past year:" + year)

5. The current time is 24 hours.

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")

String nowDate=format.format (new Date ())

System.out.println (nowDate)

6. The start time of the day

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

String start = format.format (c.getTime ()) + "00:00:00"

System.out.println (start)

7. The deadline of the day

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar calendar = Calendar.getInstance ()

String end = format.format (calendar.getTime ()) + "23:59:59"

System.out.println (end)

8. The start of the week before the current time

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

C.add (Calendar.DAY_OF_MONTH,-6)

String start = format.format (c.getTime ()) + "00:00:00"

System.out.println (start)

9. One month before the date of the day

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

C.add (Calendar.MONTH,-1); / / get the previous month

String start = format.format (c.getTime ()) + "00:00:00"

System.out.println (start)

10. The year before the start of the current time

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

C.add (Calendar.YEAR,-1); / / year minus 1

String start = format.format (c.getTime ()) + "00:00:00"

System.out.println (start)

11. The setfirstdayofweek () method of Monday time and weekend time of the current time

SimpleDateFormat format = new SimpleDateFormat ("YYYY-MM-dd")

Calendar c = Calendar.getInstance ()

C.set (Calendar.DAY_OF_WEEK,Calendar.MONDAY)

String weekStart = format.format (c.getTime ()) + "00:00:00"

System.out.println (weekStart)

Calendar ca = Calendar.getInstance ()

Ca.setFirstDayOfWeek (Calendar.MONDAY)

Ca.set (Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek () + 6); / / Sunday

String weekEnd = format.format (ca.getTime ()) + "23:59:59"

System.out.println (weekEnd)

twelve。 The first and last day of the month in which the current tense is located

SimpleDateFormat format=new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

C.set (Calendar.DAY_OF_MONTH,1); / / set to the 1st, and the current date is the first day of this month.

String monthStart = format.format (c.getTime ()) + "00:00:00"

System.out.println (monthStart)

Calendar ca = Calendar.getInstance ()

Ca.set (Calendar.DAY_OF_MONTH, ca.getActualMaximum (Calendar.DAY_OF_MONTH))

String monthEnd = format.format (ca.getTime ()) + "23:59:59"

System.out.println (monthEnd)

13. The start time of the year in which the current time is located

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd")

Calendar c = Calendar.getInstance ()

C.set (c.get (Calendar.YEAR), 0,1); / / start time date

String yearStart = format.format (c.getTime ()) + "00:00:00"

System.out.println (yearStart)

Calendar ca = Calendar.getInstance ()

Ca.set (ca.get (Calendar.YEAR), 11, ca.getActualMaximum (Calendar.DAY_OF_MONTH)); / / end date

String yearEnd = format.format (ca.getTime ()) + "23:59:59"

System.out.println (yearEnd)

14. To calculate the time difference of two microseconds.

/ / get millisecond time difference

Long val = calendarEnd.getTimeInMillis ()-calendarBegin.getTimeInMillis ()

/ / the number of days after conversion

Long day = val / (1000 * 60 * 60 * 24)

15. Get a certain time last Monday and Sunday

Calendar cal = Calendar.getInstance ()

/ / n is the number of weeks postponed, 1 this week,-1 is postponed one week forward, 2 next week, and so on

Int n = 1

String monday

Cal.add (Calendar.DATE, nasty 7)

/ / A few Calendar.MONDAY (TUESDAY...) will be spread here if you want to see the day of the week.

Cal.set (Calendar.DAY_OF_WEEK,Calendar.MONDAY)

Monday = new SimpleDateFormat ("yyyy-MM-dd") .format (cal.getTime ())

System.out.println (monday)

At this point, I believe you have a deeper understanding of "how to use java to get the time of the previous week, last month, and the previous year". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report