Amit625
Amit625

Reputation: 25

Getting the month and day from a TimeStamp

I have a String timeStamp in this format "Wed, 29 Jan 2014 20:14:15 GMT". I want to be able to compare this date with another date that is in this format 01/25/1999. I have tried simpledateformatter but with no success.

String a = connection.getHeaderField("Last-Modified"); //Returns Wed, 29 Jan 2014 20:14:15 GMT
Date lastModDate = new Date(file.lastModified()); //returns 01/25/1999

This is the simpleDateFormatter I tried implementing

SimpleDateFormat formatter;
Date dateIn = null;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try{
dateIn = (Date)formatter.parse(a);

}catch(ParseException e){
e.printStackTrace();
}
Log.d(TAG, "The server date is formated to : " + dateIn);

The dateIn is always null.

I want the to be able to do something like this

SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {

}

Upvotes: 1

Views: 119

Answers (4)

Hamid Shatu
Hamid Shatu

Reputation: 9700

Use the following code...you will get the problem right.

Calendar calender = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

String timeStamp = formatter.format(calender.getTime());
Date strDate = formatter.parse(timeStamp);

String currentTimeStamp = formatter.format(new Date());
Date currentTime = formatter.parse(currentTimeStamp);

if (currentTime.after(strDate)) {

}

Upvotes: 1

Matej
Matej

Reputation: 1757

The reason is that you are using the wrong date format for your formatter. If the date you receive looks like

"Wed, 29 Jan 2014 20:14:15 GMT"

Then you should use the following format

formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);

Upvotes: 1

Alessandro Roaro
Alessandro Roaro

Reputation: 4733

You should use the Calendar class and its subclass GregorianCalendar. For exampe, to get the month of your date:

Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.get(Calendar.MONTH); 

Upvotes: 0

celezar
celezar

Reputation: 442

Don't know what you tried but this should work:

String a = connection.getHeaderField("Last-Modified");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(a);

This can help http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 1

Related Questions