-1

I have a question on how to display the date into european style, like from 4/4/2013 to 4 - 4 - 2013. How can I fix this? Any help will be greatly appreciated. When I run my program I get this:

run:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 47
at java.lang.String.substring(String.java:1907)
at Testing1.convertDate(Testing1.java:7)
at Testing1.main(Testing1.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Below is my code:

Testing1.java:

public class Testing1{

public static String convertDate(String amerDate){
    String month =  amerDate.substring(0, '/');
    String date =  amerDate.substring('/', '/');
    int year =  amerDate.indexOf("\4d");
    return date + " - " + month + " - " + year;
}
public static void main(String args[]){
    String date = "4/4/2013";
    date = convertDate(date);
    System.out.println(date);
}
}
user2967353
  • 257
  • 3
  • 9
  • 18
  • `date = date.replace('/','-')` ? If you're getting a `Date` object, using a `DateFormat` is the way to go. – Alexis C. Dec 19 '13 at 20:02
  • Consider using built-in stuff like `SimpleDateFormat` instead: the loathsome cliche: don't reinvent the wheel. – Bathsheba Dec 19 '13 at 20:02

1 Answers1

1

Use SimpleDataFormat. To get a date like 4-4-2013 do this:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = formatter.format(yourDate);

check out the docs for more info.

Alessandro Roaro
  • 4,665
  • 6
  • 29
  • 48