1

What i'm wanting to do is format the string 08-18 15:00 to read 18-08-12 15:00. How do I convert it into a date format it and the convert back to a string so i can use to setText to a textview?

Luke Batley
  • 2,384
  • 10
  • 44
  • 76

2 Answers2

4

This is what you are looking for. You have to use SimpleDateFormat. Here is the link: String to date conversion

Community
  • 1
  • 1
pixelscreen
  • 1,945
  • 2
  • 18
  • 40
2

Of course, using SimpleDateFormat would be better solution, but if you insist and if you know coming style... Then you can simply format it yourself, with some codes like this:

public static String ConvertDate(String valueToFormat) {
      String value = valueToFormat;
      String day = value.substring(value.indexOf("-") + 1, value.indexOf(" "));
      String month = value.substring(0, value.indexOf("-"));
      String time = value.substring(value.indexOf(" ") + 1, value.lenght());

      Calendar c = Calendar.getInstance();
      int year = c.get(Calendar.YEAR);

      // if you want year to appear as only last two items
      year = year % 100;

      value = day + "-" + month + "-" + year + " " + time;
      return value;
}
yahya
  • 4,810
  • 3
  • 41
  • 58