0

I am encountering an issue which is related to Java Date Function.

I'm getting the date from Application (example: 6/5/18) which is in MM/DD/YY format. Now I need to do -2 from the date. I know how to do -2 from current system date using calendar object (see the below code).

Calendar cal = Calendar.getInstance();      
cal.add(Calendar.DATE,-2);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
String PastDate = dateFormat.format(cal.getTime());
info("Date is displayed as : "+ PastDate );

I'm not able to put the date which I'm getting from Application in this format. Can someone please help me? (Any other way to do it would also be fine)

Chandrima
  • 11
  • 1
  • Which version of Java are you using? 1.8? – Amal Gupta Jun 06 '18 at 07:42
  • I guess you have to build a Calendar object from the date you're getting from the application – Oneiros Jun 06 '18 at 07:43
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. `Calendar` is outdated too. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 06 '18 at 08:39

3 Answers3

3

I suggest you to use Java 8 compatible Date and Time types.

If you use java.time.LocalDate then this is the solution:

LocalDate.now().minusDays(2)
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
zappee
  • 20,148
  • 14
  • 73
  • 129
  • Thank you for the answer. I am doing it in OATS tool where I can't import this package - java.time.LocalDate. Also, I will have a particular date to do subtraction. LocalDate.now().minusDays(2) will give the 2 days older date from current date. This is not really the solution to my problem. If you can guide me more on this, it would really be helpful. – Chandrima Jun 06 '18 at 08:49
1

From your question, it seems that you have the challenge in dealing with formatting, and then doing the subtraction.

I would recommend Java Date and Time Apis for this purpose, using a formatter.

A junit method to achieve your requirement is given below

@Test
public void testDateFormatUsingJava8() {
    CharSequence inputdateTxt = "6/5/18";
    DateTimeFormatter  formatter = DateTimeFormatter.ofPattern("M/d/yy");
    LocalDate inputDate = LocalDate.parse(inputdateTxt, formatter);
    System.out.println(inputDate.minusDays(2L).format(formatter));
}

@Test
public void testDateCalenderUsingStringSplit() {
    String inputdateTxt = "6/5/18";
    String[] dateComponenets = inputdateTxt.split("//");
    Calendar cal = Calendar.getInstance();
    //Know where are the year month and date are stored.
    cal.set(Integer.parseInt(dateComponenets[2]), Integer.parseInt(dateComponenets[0]), Integer.parseInt(dateComponenets[2]) );
    cal.add(Calendar.DATE,-2);
    DateFormat dateFormat = new SimpleDateFormat("M/d/yy");
    String pastDate = dateFormat.format(cal.getTime());
    System.out.println("Date is displayed as : "+ pastDate );
}

@Test
public void testDateCalenderUsingJavaUtilDateApi() throws ParseException {
    String inputdateTxt = "6/5/18";
    DateFormat dateFormat = new SimpleDateFormat("M/d/yy");
    Date date = dateFormat.parse(inputdateTxt);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE,-2);
    String pastDate = dateFormat.format(cal.getTime());
    System.out.println("Date is displayed as : "+ pastDate );

The reason why I use "M/d/yy" is because your question does not pad the date and month fields in the input date with a zero. If there is a guarantee that you receive a padded value in the date and month field, using "MM/dd/yy" is suggested.

See the following answer for your reference :

DateTimeFormatterSupport for Single Digit Values

EDIT: considering the limitation to not use Java 8 Date Time APIs, I have added two other alternatives to solve the problem. The OP is free to choose any one of the solutions. Kept the Java 8 solution intact for information purposes.

Amal Gupta
  • 452
  • 4
  • 13
  • This is until now the best and most precise answer to the question. One might consider the method name `random` a bit — random? – Ole V.V. Jun 06 '18 at 08:41
  • Thanks for the answer. Yeah, I also agree with the fact that this is the most precise answer to my query. I haven't used Java Date and Time because the version of OATS, I will not be able to use this. Your answer looks perfect to me, but I'm not able to use it. – Chandrima Jun 06 '18 at 09:00
  • @OleV.V. thanks. Agreed. The method name was a bit too random. Have modified it to suit the context. It was a result of a quick tryout in one test class of the feature branch code.. :) – Amal Gupta Jun 06 '18 at 09:07
  • @Chandrima, I have added alternative solutions too. choose the one you like! – Amal Gupta Jun 06 '18 at 09:29
  • @Amal - Thanks a lot for the solution. I have taken the third option and it worked. – Chandrima Jun 06 '18 at 09:59
  • @Chandrima Great to know that. Can you mark this solution as an answer? – Amal Gupta Jun 06 '18 at 10:01
0
    Calendar cal = Calendar.getInstance();
    cal.set(2018, 5, 6); // add this, setting data from the value you parsed
    cal.add(Calendar.DATE,-2);
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
    String PastDate = dateFormat.format(cal.getTime());
    System.out.println("Date is displayed as : "+ PastDate);
Dmitri Gudkov
  • 2,093
  • 16
  • 15