存储当前日期和日期1年从当前在java [英] store current date and date 1 year from current in java
本文介绍了存储当前日期和日期1年从当前在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.util。日期;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Scanner;
import java.util.Calendar;
这是我当前的日期:
DateFormat newDate = new SimpleDateFormat(MM / dd / yyyy);
Date date = new Date();
startDate = newDate.format(date);
所以如果今天是例如它将存储2/18/2013。我试图存储日期2/18/2014。如何不要这样做?
解决方案
如果不想拖动外部库,只需使用 calendar.add(Calendar.YEAR,1)
日历cal = Calendar.getInstance() ;
日期今天= cal.getTime();
cal.add(Calendar.YEAR,1); //获得上一年添加-1
Date nextYear = cal.getTime();
注意,如果日期是 29 / Feb / 2012
你加了1年,你会得到 28 / Feb / 2013
I have everything setup already to store the current date to a variable in Java. What I am trying to figure out is how to store a date of 1 year after the current date.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Scanner;
import java.util.Calendar;
Here is what I have for the current date:
DateFormat newDate = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
startDate = newDate.format(date);
So if it were today for example it would store 2/18/2013. I am trying to store the date 2/18/2014. How would I go about doing this?
解决方案
If you do not want to drag external libraries, just use calendar.add(Calendar.YEAR, 1)
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, 1); // to get previous year add -1
Date nextYear = cal.getTime();
Note, if the date was 29/Feb/2012
and you added 1 year, you will get 28/Feb/2013
这篇关于存储当前日期和日期1年从当前在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文