-3

Im trying to set the Calendar date object in the following way, but I don't know how.

public void setTradeDate(String tradeDate) {
    Calendar tradeDate = ?
}

The String is in the format:

String tradeDate = "2017-06-01T15:49:18Z";
Syed
  • 2,471
  • 10
  • 49
  • 89

4 Answers4

1

Are you able to use java-8?

ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(DateTimeFormatter.ISO_INSTANT.format(dateTime));
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • Im using Java 7.. :( \ – Syed Aug 17 '17 at 11:13
  • @Syed, can you then use http://www.joda.org/joda-time/ in your project? – Anton Balaniuc Aug 17 '17 at 11:15
  • Anton, I should get the calendar date object. – Syed Aug 17 '17 at 11:17
  • Quotes from your link, @AntonBalaniuc: “Users are now asked to migrate to `java.time` (JSR-310).” “Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to `java.time` (JSR-310).” While Joda-Time is viable, I think that ThreeTen Backport, suggested [by Hugo](https://stackoverflow.com/a/45736295/5772882) is even better. – Ole V.V. Aug 18 '17 at 08:51
1

To create a Calendar from a String, you can use a SimpleDateFormat (as already suggested by the other answers). The formatter will parse the String and create a Date object, which will be set to the Calendar:

String tradeDate = "2017-06-01T15:49:18Z";

// create Calendar
Calendar cal = Calendar.getInstance();
// create formatter
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
// parse string and set the resulting date to Calendar
cal.setTime(df.parse(tradeDate));

The Calendar will contain the date equivalent to 2017-06-01T15:49:18Z. Note that this date/time is in UTC - the Z in the end is the UTC designator.

But if you try to print this date (using System.out.println(cal.getTime()), a logger, or even checking its value in a debugger), it'll implicity use the toString() method, and this converts the fields (day, month, year, hour, minute, seconds, etc) to the system's default timezone (if your default timezone is in India, for example, the date will be printed as Thu Jun 01 21:19:18 IST 2017, although the internal value won't be changed). But the value will still be equivalent to the UTC input.

Don't be misleaded by the output of toString() method from Calendar and Date classes. What matters is the value of the timestamp: the number of milliseconds since 1970-01-01T00:00Z, which can be checked with cal.getTimeInMillis(). Check this article for more information.


As you're using Java 7, there's another (better) alternative: you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes.

You can parse the input to a org.threeten.bp.ZonedDateTime and use the org.threeten.bp.DateTimeUtils class to convert it to a java.util.Calendar:

String tradeDate = "2017-06-01T15:49:18Z";
// parse input
ZonedDateTime zdt = ZonedDateTime.parse(tradeDate);
// convert to calendar
Calendar cal = DateTimeUtils.toGregorianCalendar(zdt);

Using this backport eliminates lots of problems and design issues of the old Calendar API. And makes a future migration to Java 8 much easier, as in new Java 8 API the classes and methods names are the same, just the packages are different (in Java 8 is java.time and in ThreeTen Backport is org.threeten.bp).

0

Read the javadoc of SimpleDateFormat.
'T' will yield a literal T. X will yield a ISO 8601 time zone (which is Z in case of UTC time zone).
So you can achieve it by:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar calobj = Calendar.getInstance();
System.out.println(df.format(calobj.getTime()));

The X is not supported before Java 7. You can use 'Z' instead to get a literal Z.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • Again it will be in Dateformat return type. How can I get only the calendar object type. Because I need to set the calendar object – Syed Aug 17 '17 at 11:22
  • this is my method signature and it takes calendar object. public void setTradeDate(java.util.Calendar tradeDate) { } – Syed Aug 17 '17 at 11:26
  • Actually, `X` is [supported in Java 7](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). It's [not supported in Java 6](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html), though. –  Aug 17 '17 at 12:26
  • 1
    Thanks @Hugo , I updated my answer. – Thomas Fritsch Aug 17 '17 at 12:51
-1

A possible solution is

public void setTradeDate(String tradeDate) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
    try {
        cal.setTime(df.parse(tradeDate));
    } catch(ParseException e) {
        //Handle exception
    }
    System.out.println(cal); //Change to setting the field instead.
}

use it by callling

setTradeDate("2017-06-01T15:49:18Z");
Jotunacorn
  • 496
  • 1
  • 4
  • 12
  • I require in Calendar return type.. Please read my question – Syed Aug 17 '17 at 11:15
  • I've read your question and as I understand it the current System.out.println(df.format(calobj.getTime())); prints on the wrong format. The solution to this is to change the format as I instructed. What do you mean by "I require Calendar return type"? – Jotunacorn Aug 17 '17 at 11:24
  • public void setTradeDate(java.util.Calendar tradeDate) { }.. My method takes calendar object in that format. – Syed Aug 17 '17 at 11:25
  • DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); System.out.println(df.format(tradeDate.getTime())) – Jotunacorn Aug 17 '17 at 11:26
  • @Jotunacom, you are getting my point wrong.. I have sent the signature to tell u that I require 2017-06-01T15:49:18Z as Calendar object so that I can set in that method. – Syed Aug 17 '17 at 11:28
  • So what you want to do is convert a String time to a calendar? Calendar cal = Calendar.getInstance(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); cal.setTime(df.parse("2017-06-01T15:49:18Z")) see https://stackoverflow.com/questions/5301226/convert-string-to-calendar-object-in-java – Jotunacorn Aug 17 '17 at 11:32
  • 1
    Please test your code again. There is parseException – Syed Aug 17 '17 at 11:36