Luke Batley
Luke Batley

Reputation: 2402

Android date formatting a string

Does anyone know how to format a string that contains a Date?

i have a string that includes a date and time that has been passed from a Json feed.

which looks like this

2012-06-11 14:00

the problem with this is its the wrong way round how do i format it so it becomes two strings so i can add bits inbetween for example. Does anyone know how i can do this?

"at " + time + " " + "on " + "11-06-2012"

at 14:00 on 11-06-2012

Upvotes: 0

Views: 112

Answers (2)

Sudar Nimalan
Sudar Nimalan

Reputation: 3992

     try {
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("'at' HH:mm 'on' dd-MM-yyyy");
            simpleDateFormat2.format(simpleDateFormat1.parse("2012-06-11 14:00"));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33238

try this one..

SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd hh:mm");
java.util.Date date = null;
try 
{
     date = form.parse(string);
}
     catch (ParseException e) 
{
}
SimpleDateFormat postFormater = new SimpleDateFormat("dd-MM-yyyy");
String newDateStr = "at"+date.getHours()+":"+date.getMinutes()+"on"+postFormater.format(date);

Upvotes: 3

Related Questions