Problem:
I need to pass Date class Object to a function and that Date Object should contain one Day ahead of the System Time.
For Ex:
If Today's Date is 2017-04-20 17:01:31.Then,Date Object should contain 2017-04-21 17:01:31
Is it possible to store a specified format into Date Class Object and pass into it.
I tried the following thing and it didn't work.
Can anyone guide me if it is possible through Code or should I use SQL Query Concept to add a Day.
Below is my Code
public static void main(String[] args) throws ParseException {
String dateFormat="yyyy-MM-dd HH:mm:ss";
String s2=addDate(dateFormat);
convertStringToDate(s2,dateFormat);
}
public static Date convertStringToDate(String dateInStr, String dateFormat) throws ParseException
{
FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);//("yyyy-MM-dd HH:mm:ss");
Date date = null;
date = fdf.parse(dateInStr);
System.out.println("From convertStringToDate ");
System.out.println(date);
return date;
}
public static String addDate(String dateFormat) throws ParseException{
FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE,1);
String s1=fdf.format(c.getTime());
System.out.println("From addDate ");
System.out.println(s1);
return s1;
}
Expected Output from convertStringToDate:
2017-04-21 17:01:31
OutputShown from convertStringToDate:
Fri Apr 21 17:01:31 IST 2017
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…