| |

VerySource

 Forgot password?
 Register
Search
View: 731|Reply: 2

anxious! !! !! How to find the current date minus one month and the corresponding date

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-12 10:30:01
| Show all posts |Read mode
How to find the current date minus one month, the corresponding date. Such as today 2017-01-05, minus one month is 2016-12-05.
2017-3-31 minus one month should be 2017-02-28

What should I do? ? ? ? ,First, thank you
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

Post time: 2020-6-9 15:00:01
| Show all posts
import java.util.Date;

public class test1111
    {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
            java.util.Calendar c = new java.util.GregorianCalendar();
            c.setTime(new Date());
            System.out.println("Year:Month:Day"+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c. DATE));
            c.add(c.MONTH,-1);
            System.out.println("Year:Month:Day"+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c. DATE));
}

    }




After adjusting the system date to 2017/03/31, the results are as follows:
Year:Month:Day2017-3-31
Year:Month:Day2017-2-28
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-13 21:45:01
| Show all posts
package com.ice.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
public static String getStringDateFormat(Date date, String dateformat) {
SimpleDateFormat sf = new SimpleDateFormat(dateformat);
return sf.format(date);
}
public static Date getRMDate(Date nowdate) {
Calendar c=Calendar.getInstance();
c.setTime(nowdate);
System.out.println(DateUtil.getStringDateFormat(c.getTime(), "yyyy-MM-dd"));
c.add(c.MONTH, -1);
System.out.println(DateUtil.getStringDateFormat(c.getTime(), "yyyy-MM-dd"));
return new Date();
}
public static void main(String[] args) throws ParseException {
Date date =new Date();
DateUtil.getRMDate(date);
}

}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list