Skip to content Skip to sidebar Skip to footer

How To Format A Date Android?

I have a date in String an I using this: SimpleDateFormat sdf = new SimpleDateFormat('dd/MM/yyyy HH:mm'); dataUltima = sdf.format(sdf); to format the date. But this method return

Solution 1:

I am not sure what is your problem.

Try it. Maybe help.

Stringserver_format="2013-01-02";    //server comes format ?SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
try {

    Datedate= sdf.parse(server_format);
    System.out.println(date);
    Stringyour_format=newSimpleDateFormat("dd/MM/yyyy").format(date);
    System.out.println(your_format);    //you want format ?

} catch (ParseException e) {
    System.out.println(e.toString()); //date format error
}

Solution 2:

Ty following:

try
{
  //create SimpleDateFormat object with source string date format
  SimpleDateFormat sdfSource = new SimpleDateFormat("yyy-dd-MM");

  //parse the original date string(strDate) that you are getting from server into Date object
  Date date = sdfSource.parse(strDate);

  //create SimpleDateFormat object with desired date format
  SimpleDateFormat sdfDestination = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

  //parse the date into another format
  strDate = sdfDestination.format(date);

  System.out.println("Date is converted");
  System.out.println("Converted date is : " + strDate);

}
catch(ParseException pe)
{
  System.out.println("Parse Exception : " + pe);
}

Source: Convert date string from one format to another format using SimpleDateFormat

Hope this helps.

Solution 3:

Try this code:

DatenewDate=newDate();

    // Print the date with the server formatSimpleDateFormatsdfServer=newSimpleDateFormat("yyy-dd-MM ");
    System.out.println("Server date " + sdfServer.format(newDate));

    // Create a new formatter to get the date in the format you wantSimpleDateFormatsdf=newSimpleDateFormat("dd/MM/yyyy HH:mm");
    StringdataUltima= sdf.format(newDate);
    System.out.println("System date " + dataUltima);

Solution 4:

Try this:

Stringdate="2011-11-12 11:11:11"; //Fill with sample date received from serverSimpleDateFormatsdf=newSimpleDateFormat("yyyy-dd-MM HH:mm:ss");
DatetestDate=null;
try {
    testDate = sdf.parse(date);
}catch(Exception ex){
    ex.printStackTrace();
}
SimpleDateFormatformatter=newSimpleDateFormat("dd/MM/yyyy hh:mm");
StringdataUltima= formatter.format(testDate);
System.out.println("Date is "+dataUltima );

Post a Comment for "How To Format A Date Android?"