1

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 the actual date of system. How can I only format the date without change the date.

Obs. I'm getting this date from a server, it come to me yyy-dd-MM and I want it dd/MM/yyyy

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Roland
  • 826
  • 2
  • 9
  • 24

4 Answers4

2

I am not sure what is your problem.

Try it. Maybe help.

String server_format = "2013-01-02";    //server comes format ?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {

    Date date = sdf.parse(server_format);
    System.out.println(date);
    String your_format = new SimpleDateFormat("dd/MM/yyyy").format(date);
    System.out.println(your_format);    //you want format ?

} catch (ParseException e) {
    System.out.println(e.toString()); //date format error
}
shuyu
  • 136
  • 4
0

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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I'm getting a error, nullpointexception 07-16 14:41:10.709: E/AndroidRuntime(28291): Caused by: java.lang.NullPointerException 07-16 14:41:10.709: E/AndroidRuntime(28291): at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001) 07-16 14:41:10.709: E/AndroidRuntime(28291): at java.text.DateFormat.parse(DateFormat.java:624) 07-16 14:41:10.709: E/AndroidRuntime(28291): at com.example.Tappa.SegundaTelaActivity.onCreate(SegundaTelaActivity.java:171) – Roland Jul 16 '13 at 17:42
  • Which is `171` line in your activity? Also make sure that `strDate` is not null before you pass it into this for converting date. Are you logging the date which comes from your server. Is it non-null? – Shobhit Puri Jul 16 '13 at 17:45
0

Try this:

String date = "2011-11-12 11:11:11"; //Fill with sample date received from server
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
Date testDate = null;
try {
    testDate = sdf.parse(date);
}catch(Exception ex){
    ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
String dataUltima = formatter.format(testDate);
System.out.println("Date is "+dataUltima );
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • I'm getting a error to Caused by: java.lang.IllegalArgumentException 07-16 14:50:00.239: E/AndroidRuntime(29237): at java.text.DateFormat.format(DateFormat.java:365) 07-16 14:50:00.239: E/AndroidRuntime(29237): at java.text.Format.format(Format.java:93) – Roland Jul 16 '13 at 17:52
  • What is your server date coming as? This works for me if the date that you get from your server is of the form "yyy-dd-MM" only with any time component . Is this how it comes ? If not it will throw an error – Vrashabh Irde Jul 17 '13 at 06:23
  • It comes for me as a String: "yyyy-dd-MM HH:mm:ss" and I want it dd/MM/yyy HH:mm – Roland Jul 17 '13 at 12:37
  • Oh in your question you said just yyy-dd-mm. Then you have to format it as is. edited – Vrashabh Irde Jul 17 '13 at 17:03
0

Try this code:

    Date newDate = new Date();

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

    // Create a new formatter to get the date in the format you want
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    String dataUltima = sdf.format(newDate);
    System.out.println("System date " + dataUltima);
amatellanes
  • 3,645
  • 2
  • 17
  • 19