-1

My method is :

public String changeCurrentDate(Integer variant){
    String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
    String currentDate = currentTime.substring(0, 10);
    System.out.println("currentDate " +currentDate);
    String date = null;
    DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    try{
        Date date3 = df.parse(currentDate);
        df.format(date3);
        System.out.println("date3 " +date3);
        Date previousDate = DateUtils.addDays(date3, variant);
        date = previousDate.toString();
        return date;
    }catch (Exception e){
    }
    return date;
}

Note : currentTime variable always have the value like "18/12/2017" I'm expecting result of date in dd/mm/yyyy format. but it always gives "Wed Jan 18 00:12:00 IST 2017" like this.

Run Time Results :

currentDate 18/12/2017
date3 Wed Jan 18 00:12:00 IST 2017
ChathuNM
  • 3
  • 2
  • 1
    Dates themself do not have a format in java, when you call `df.format(date3);` this method will return a String that is formatted by your SimpleDateFormat but it will not change anything about `date3` at all. So calling format without doing anything with the return value is pointless. – OH GOD SPIDERS Dec 18 '17 at 12:33
  • 1
    Why do you think `mm` stands for month? – Tom Dec 18 '17 at 12:45
  • I recommend you don’t use the long outdated `Date` class and particularly avoid the notoriously troublesome `SimpleDateFormat` class. [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Dec 18 '17 at 13:30

3 Answers3

1

You should return the formatted date, not the toString() of the date. Try this:

Date previousDate = DateUtils.addDays(date3, variant);
return df.format(previousDate);
Alexcocia
  • 128
  • 9
0

df.format simply returns a String representation of the Date with the format applied, therefore the line you have in your code has no effect.

Try changing you code to use the formatted output instead:

public String changeCurrentDate(Integer variant){
    String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
    String currentDate = currentTime.substring(0, 10);
    System.out.println("currentDate " +currentDate);
    DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    Date date3 = df.parse(currentDate);
    System.out.println("date3 " + df.format(date3));
    Date previousDate = DateUtils.addDays(date3, variant);
    return previousDate.toString();
}

Also - its bad to catch Exception, so you should remove that.

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
0

We should always try to find out the more convenient way so that we can further use it. In that case, we can use below method:

public String dateString(String input) {        
    SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
    String formattedDate = "";        
    try {
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        formattedDate = formatter.format(date);         
    } catch (ParseException e) {            
        e.printStackTrace();
    }

    return formattedDate;
}

You can easily call this method like below:

Date date3 = df.parse(currentDate);
df.format(date3);

String input = date3.toString();            
String requiredDate = dateString(input);

System.out.println("requiredDate: "+ requiredDate);

Which returns the output like below:

requiredDate: 18/12/2017
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • @ChathuNM Hey brother! don't you want to use the more convenient and reusable method? If so, then you can think and decide once more. – Ali Azam Dec 18 '17 at 13:12
  • I strongly suggest that the more convenient way goes through `java.time`, the modern Java date and time API. – Ole V.V. Dec 18 '17 at 13:38