I know these type of question asked lot's of time on lot's of website. Till I not got solution that's why I am putting this question here.
I want year, month, date, date and time from date format
Tue, 12 Jan 2016 09:40:07 GMT
I am getting this date format form HttpResponse header and need year, month, date, date from this date format.
Following code I am using but not getting real value:
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date date;
try {
date = dateFormat.parse(header.getValue());// here we are getting date in format "Tue, 12 Jan 2016 09:40:07 GMT"
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("YEAR: " + calendar.YEAR);
System.out.println("MONTH: " + calendar.MONTH);
System.out.println("DATE: " + calendar.DATE);
} catch (ParseException e) {
e.printStackTrace();
}
I tried this also:
1.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
2.
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
But all the above code give following result:
YEAR: 1
MONTH: 2
DATE: 5
You are using calendar.YEAR
, calendar.MONTH
, and calendar.DATE
the Calendar class contains static YEAR, MONTH and DATE
variables so it print value of these variable i.e 1,2 and 5.
Instead of it use this:
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));