Simplest way to convert Java Calendar to ISO

1.8k views Asked by At

I've got a Calendar that comes back in a following format (it's in Java Calendar type): 2020-02-15T00:00:00. What's the simplest, shortest way to convert it to the following Calendar like this one and persist the Calendar type: Mon Nov 05 2018 14:08:58 GMT+0000 (Greenwich Mean Time)?

In JavaScript we can use something like var date = new Date();

4

There are 4 answers

2
findusl On

Assuming you have an instance of Calendar cal, you should be able to do this:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");
String result = dateFormat.format(cal.getTime());

getTime gives you a date for the SimpleDateFormat.

This is the simplest way to deal with a Calendar object. Also the most error prone one. I do agree with all the comments stating you should use java.time, but who am I to tell you what to do.

2
Systemmanic On

Here is an example using a String converted into your example format, using the Date API introduced in Java 8:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

class Scratch {
    public static void main(String[] args) {

        String input = "2020-02-15T00:00:00";

        LocalDateTime parsedTime = LocalDateTime.parse(input);

        ZonedDateTime zonedDateTime = ZonedDateTime.of(parsedTime, ZoneId.of("GMT"));

        String output = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss Z '('z')'")
                .format(zonedDateTime);

        System.out.print(output);
    }
}
2
Basil Bourque On

Date-time objects have no “format”

I've got a Calendar that comes back in a following format (it's in Java Calendar type): 2020-02-15T00:00:00.

No, you don’t.

A Calendar object has no format. Only text representing a date-time value has a format.

following Calendar like this one and persist the Calendar type: Mon Nov 05 2018 14:08:58 GMT+0000 (Greenwich Mean Time)?

There is no such thing as a Calendar object with that format, as a Calendar has no format and is not text.

Learn to search Stack Overflow before posting. This has been covered many times already.

java.time

The terrible Calendar class was supplanted years ago by the java.time class with the adoption of JAR 310. Never use Calendar.

Get the current time in UTC with the OffsetDateTime class.

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;

To generate text in standard ISO 8601 format, call toString. To persist a date-time value as text, use these formats – that’s why they were invented.

To generate text in another format, use the DateTimeFormatter class. This has been covered many many times, so search Stack Overflow.

Convert

If given a GregorianCalendar object, convert immediately to a ZonedDateTime object by calling new methods added to the old legacy classes.

ZonedDateTime zdt = myGregCal.toZonedDateTime() ; 

Cast if need be.

ZonedDateTime zdt = ( (GregorianCalendar) myCal ).toZonedDateTime() ; 

Then use DateTimeFormatter to generate text in your desired format.

3
betontalpfa On

You can use SimpleDateFormat

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class HelloWorld
{
  public static void main(String[] args)
  {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    String dateString = "2020-02-15T00:00:00";
    try {
        Date parsed = format.parse(dateString);
        System.out.print(parsed.toString());
        }
        catch(ParseException pe) {
            System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
    }
  }
}