0

How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android .

The actual value I get from server is in yr-mnth-day format. But I need to display it as day-mnth-yr in my app.How can I achieve this?

Ganga
  • 17
  • 5
  • 1
    Use SimpleDateFormat: [http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – daemmie Aug 25 '15 at 08:53
  • @oberflansch: can you give me a sample example? – Ganga Aug 25 '15 at 08:54

4 Answers4

1

First you'll need to parse the String into a Date. Then you format the date according to your needs, the example below is an edited version of BalusC's answer from this topic: Change date format in a Java string

String oldstring = "2015-05-12";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
//Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.

String newstring = new SimpleDateFormat("dd-MM-yyyy").format(date);
System.out.println(newstring); // 12-05-2015
Community
  • 1
  • 1
Frank D.
  • 1,306
  • 1
  • 13
  • 23
0

If you say this, i suppose you mean in a string, so you can convert string to Date with

String dateServer = "2015-05-12";
//Identify the format which has been used from server
DateFormat format = new SimpleDateFormat("yyyy, MM, dd", Locale.ENGLISH);
//Convert string to Date using this format
Date date = format.parse(string);
//Create the format you need to print
DateFormat wellFormatted = new SimpleDateFormat("dd/MM/yyyy");
//now convert the date into a string formatted as you like
String wellPrintedDate = wellFormatted.format(date);

that's all, "wellPrintedDate" is the string in format day/month/yeah. here you can find the list of code used for formatting :)

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
0
try {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date past = format.parse("2015-05-12");
        SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println("dt=" + format1.format(past));
    } catch (ParseException e) {
        e.printStackTrace();
    }
Ravi
  • 34,851
  • 21
  • 122
  • 183
0
  public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
    if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
        inputFormat = "yyyy-MM-dd";
    }
    if(outputFormat.equals("")){
        outputFormat = "dd-mm-yyyy"; // if inputFormat = "", set a default output format.
    }
    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    // You can set a different Locale, This example set a locale of Country Mexico.
    //SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
    //SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);
    } catch (Exception e) {
        Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
    }
    return outputDate;

}

call like this

  Log.e("",""+formattedDateFromString("yyyy-MM-dd","dd-MM-yyyy","2015-05-12"));


output:  12-05-2015
RamBabu Pudari
  • 912
  • 7
  • 19