Ruchir
Ruchir

Reputation: 1122

How to change date format coming from database to android

In my android application, I'm parsing data from mysql database using JSON and displaying in android listview. In mysql database, the date format is YYYY-MM-DD and I am getting this format in android using JSON as a String. Is it possible to change date format to DD-MM-YYYY in android instead of database..??

Upvotes: 1

Views: 1899

Answers (3)

user6440030
user6440030

Reputation:

this will be the answer i need :D

   String well=String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 
   java.util.Locale.getDefault()).format(new java.util.Date()));

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

Yes, it is. Use SimpleDateFormat :

String inputPattern = "yyyy-MM-dd";
String outputPattern = "dd-MM-yyyy";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

Date date = null;
String result = null; 

try {
    date = inputFormat.parse(time);
    result = outputFormat.format(date);
} catch (ParseException e) {
    e.printStackTrace();
}

//use the result variable as you wish

Upvotes: 1

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Use SimpleDateFormat to convert date form one format to another format :

SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY");
try{
    Date d = df1.parse(DateString);
    System.out.println("new date format " + df2.format(d));
}catch(Exception e){
   e.printStackTrace();
}

Upvotes: 1

Related Questions