1

I have a UTC time in this format 2013-03-04T14:37:15 How can I convert this into local time and then to one minute ago format.

I tried this first to convert the time to local format:

private String getDate(String date) {
        Date fDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            fDate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
return fDate.toString();
    }

But the above functions throws an exception saying unparseable date..

Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
sukarno
  • 597
  • 2
  • 10
  • 31
  • Similar to [Convert UTC to current locale time](http://stackoverflow.com/questions/6049207/convert-utc-to-current-locale-time) and [Converting UTC dates to other timezones](http://stackoverflow.com/questions/6088778/converting-utc-dates-to-other-timezones) – McArthey Mar 19 '13 at 16:40

2 Answers2

4

The parse exception you get is because of the extra T in your date(2013-03-04T14:37:15).

You can either remove that extra 'T' and parse the date with your current date format,

Or if you need to parse it with the T, change your format to parse the literal T as well,

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 1
    Works fine now am getting the formatted date like this: Mon Mar 04 20:07:15 GMT+05:30 2013, how can i change this to desired format like ex: yyyy-mm-dd 04:30 PM – sukarno Mar 19 '13 at 17:10
3

Check DateUtils.getRelativeTimeSpanString() (one methods with the same name from DateUtils). It will be something like :

Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
fDate = simpleDateFormat.parse(date);
return DateUtils.getRelativeTimeSpanString(context, System.currentTimeMillis()-fDate.getTime())'
stefan.nsk
  • 1,755
  • 1
  • 16
  • 12