0

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
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
Vinit ...
  • 1,409
  • 10
  • 37
  • 66
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Nov 25 '18 at 03:54

2 Answers2

3

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));
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
  • @Jaiprakesh..thanks...now its working... – Vinit ... Jan 12 '16 at 11:14
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Nov 25 '18 at 03:55
1

Set timezone to Calendar instance to GMT.

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(TimeZone.getTimeZone("GMT")); // set correct timezone to calendar
    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();
    }
M S Parmar
  • 955
  • 8
  • 22