-2

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();

LazioTibijczyk
  • 1,701
  • 21
  • 48
  • Have you tried anything so far? – rmlan Nov 05 '18 at 14:11
  • Just going through the SimpleDateFormat documentation and wondering why it takes so many lines of code to just convert it to ISO (or maybe I'm looking at an over complicated examples) – LazioTibijczyk Nov 05 '18 at 14:16
  • Do you mind sharing what you think is "so many lines", as in, your attempt? – rmlan Nov 05 '18 at 14:17
  • 4
    You should use the new Java Time API from the `java.time` package. The `Calendar`, `Date` and `SimpleDateFormat` classes are obsolete. – MC Emperor Nov 05 '18 at 14:18
  • 3
    I recommend you to use the new Api produced in java 8 it which is `LocalDate` it is much easier than calendar – Basil Battikhi Nov 05 '18 at 14:27
  • 2
    I can’t make sense of the question, sorry. `2020-02-15T00:00:00` is ISO 8601 format. `Mon Nov 05 2018 14:08:58 GMT+0000 (Greenwich Mean Time)` is not. Also a `Calendar` doesn’t have a format, so cannot come back in the format you say. – Ole V.V. Nov 05 '18 at 15:09
  • @OleV.V. I must have made a mistake then. I want to achieve the second format and persist the Calendar type. – LazioTibijczyk Nov 05 '18 at 15:11
  • Possible duplicate of [how to change the format of Calendar object](https://stackoverflow.com/questions/50286811/how-to-change-the-format-of-calendar-object) – Ole V.V. Nov 05 '18 at 15:14
  • 1
    The `Calendar` class is long outdated and poorly designed. Why do you want to persist that? I’d persist an `Instant` (class from java.time, the modern Java date and time API) or if that’s not feasible, then its string representation (from its `toString` method; which adheres to ISO 8601). Your problem sounds very much like an [XY problem](https://en.wikipedia.org/wiki/XY_problem), so please give more context of why you think you want that. – Ole V.V. Nov 05 '18 at 15:18

4 Answers4

1

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);
    }
}
syncdk
  • 2,820
  • 3
  • 25
  • 31
1

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Well, considering a fact I always do, I must be really poor at finding those covered many times topics. Would be helpful to at least point in the right direction. – LazioTibijczyk Nov 05 '18 at 15:31
  • @LazioTibijczyk https://duckduckgo.com/?q=site%3Astackoverflow.com+java+date+format&t=iphone&ia=web. First two hits address your topic and explain the modern *java.time* classes. – Basil Bourque Nov 05 '18 at 15:41
0

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 + "\"");
    }
  }
}
betontalpfa
  • 3,454
  • 1
  • 33
  • 65
  • Can't parse a Calendar. It's not a String. – LazioTibijczyk Nov 05 '18 at 14:47
  • But somehow you can see this value... What about `toString()` method? – betontalpfa Nov 05 '18 at 14:51
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Nov 05 '18 at 15:11
0

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.

findusl
  • 2,454
  • 8
  • 32
  • 51
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Nov 05 '18 at 15:11
  • I am also more of a fan of the new java.time api but if he gets an outdated Calendar object he might as well use the old library for brevity or? Especially if he asks for the simplest way. – findusl Nov 05 '18 at 15:12