Question

I have a string that represents a date with a format MM/dd/yyyy hh:mm:ss that looks like:

"03/26/2014 17:32:25 IST"<BR>

When I parse the string into a date as:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss zzz");
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.setTime(sdf.parse("03/26/2014 17:32:25 IST"));
String output = sdf.parse(sdf.format(cal.getTime()));

I have tried adding Locale.getDefault() in the SimpleDateFormat but it didn't work.

Any help will be appreciated.

Was it helpful?

Solution

First error: You try to case a java.util.Date (the result of parse-method) on String (compile-error).

Second error: You should use pattern symbol HH not hh (twenty-four-hour-clock according to hour input of 17h).

Third error: Set the timezone on the format object, not on Calendar (and it hopefully corresponds to timezone IST - either you are in Israel or in India).

Updated: It appears that "IST" is not a known time zone name on your Android device. The motivation of Google-Android was probably to avoid ambiguous names ("Israel Standard Time" or "India Standard Time") so Android has other different names in its resource repository. You might try text preprocessing like this workaround:

if (timestampText.endsWith(" IST")) {
  timestampText = timestampText.substring(0, timestampText.length() - 4);
}

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
java.util.Date d = sdf.parse(timestampText);

Also check the output of method DateFormatSymbols.getInstance().getZoneStrings() in order to see if Android expects another timezone name instead of "IST" (which is more wide spread on Java-VMs).

OTHER TIPS

Try Below code you need to assign output into Date data type : 

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss zzz");
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault());
    cal.setTime(sdf.parse("03/26/2014 17:32:25 IST"));
    java.util.Date output = sdf.parse(sdf.format(cal.getTime())); 

umm, something like this? Make sure you are using proper imports..

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss zzz");
        Date parse = null;
        Calendar calendar = Calendar.getInstance();
        try {
            parse = sdf.parse("03/26/2014 17:32:25 IST");
            calendar.setTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Toast.makeText(this, calendar.getTime() + "", Toast.LENGTH_SHORT).show();
    }
}

Try Below code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String date_parse="2015-04-09 05:06:14";
    try{
        SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdfSource.parse(date_parse);

        SimpleDateFormat sdfDestination = new SimpleDateFormat("MMMM dd,yyyy hh:mm a");
        date_parse = sdfDestination.format(date);

        System.out.println("Date is converted from dd/MM/yy format to MMMM dd,yyyy hh:mm a");
        System.out.println("Converted date is : " + date_parse);

    } catch(Exception e){
        System.out.println(e.getMessage());
    }
}

output: Date is converted from dd/MM/yy format to MMMM dd,yyyy hh:mm a Converted date is : April 09,2015 05:06 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top