-1

i need to remove "GMT" from a string like this one "2012-07-15 17:00 GMT". Try to use this code, but testDate returns null. Can anyone help ?

Thank you

    String date = "2012-07-15 17:00 GMT";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }catch(Exception ex){
            ex.printStackTrace();
        }
filipe
  • 23
  • 1
  • 4
  • Solved !!! Sorry guys. I have "yyyy-MMM-dd HH:mm" in my code. My mistake. Works fine when i correct the code to just MM. – filipe Jul 15 '12 at 16:34

2 Answers2

2

will this help ?

String date = "2012-07-15 17:00 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm z");
Date testDate = null;
try {
    testDate = sdf.parse(date);
}catch(Exception ex){
    ex.printStackTrace();
}
System.out.println(testDate);

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
date = sdf.format(testDate); 
System.out.println(date);
sunil
  • 6,444
  • 1
  • 32
  • 44
0

Since you apparently start with a String why not just cut off the last four characters, with String.substring() for example?

date = date.substring(0, date.length() - 4);

You could also confirm that " GMT" exists:

int index = date.indexOf(" GMT");
if(index > -1)
    date = date.substring(0, index);

If you are not changing the date's format there is no need to use the extra SimpleDateFormat and Date classes.

Sam
  • 86,580
  • 20
  • 181
  • 179