kiran guled
kiran guled

Reputation: 97

how to convert datetime string to another datetime format?

I have a column in sqlite where i am saving current datetime as string now my issue is, i am unable to format it as shown below :

This string : 2015-08-20 18:55:55 pm

to

this string : 20-Aug-2015 06:55:55 pm

thank you/

Upvotes: 0

Views: 2273

Answers (4)

MPG
MPG

Reputation: 795

use some thing like

String value = "2015-08-20 18:55:55 pm";
String result = "";
Date date = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
try{
  date = dateFormat.parse(value);
}
 catch(Exception e)
{
   e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat printFormat = new SimpleDateFormat("dd-MMMM-yyyy hh:mm:ss aaa");
result = printFormat.format(calendar.getTime());

Upvotes: 1

Irfan Ali
Irfan Ali

Reputation: 199

Simply Use this method as well

String pdate = FormatDate(pickdate);//7:28:2015

 public static String FormatDateReverse(String d){
    //3 September 2014
    SimpleDateFormat fromUser = new SimpleDateFormat("dd MMMM yyyy", Locale.US);
    SimpleDateFormat myFormat = new SimpleDateFormat("MM:dd:yyyy");
    String reformattedStr="";
    try {
    reformattedStr = myFormat.format(fromUser.parse(d));
        Log.d("Date=",reformattedStr);
    } 
    catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     return reformattedStr;
    }

Upvotes: 0

Rohit5k2
Rohit5k2

Reputation: 18112

Do this

String dateTxt = "2015-08-20 18:55:55 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
Date date = null;
try 
{
    date = sdf.parse(dateTxt);
}
catch(Exception ex)
{
    ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy hh:mm:ss aaa");
String newFormat = formatter.format(date);

Upvotes: 2

BNK
BNK

Reputation: 24114

        DateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aaa", Locale.US);
        Date fullDate = fullDateFormat.parse(longDate);  
        resultdate= new SimpleDateFormat("dd-MMM-yyyy hh:mm:sss aaa", Locale.US).format(fullDate);

Upvotes: 0

Related Questions