1

I am building a API Java code that is downloading into a CSV file a list of transactions, disputes and payments made through Paypal for this company I work for. One of the columns from the CSV file is a date related column as you can see below:

transactionData.add(String.valueOf(transaction.getDisputes().get(i).getReceivedDate()));

The issue is that all values for the data column above is coming in the CSV as a XMLGregorianDate:

java.util.GregorianCalendar[time=1589760000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2020,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=139,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]

What changes should I make to the line above to give me the data in timestamp with timezone i.e. "yyyy-mm-dd hh:mi:ss+/-tz"?

ERR
  • 213
  • 1
  • 6
  • 17
  • That is a `GregorianCalendar` object which **is** a "normal calendar" object in Java. What do you want, can you describe that in more detail than just saying "normal date as usual"? – Joachim Sauer May 27 '20 at 10:49
  • Hi @JoachimSauer thanks for your question. I just added the format that I am after. – ERR May 27 '20 at 10:51
  • 1
    @JoachimSauer `GregorianCalendar` was a normal date until Java 8. That’s six years ago by now. It was always poorly designed, and fortunately it is now long outdated. I wouldn’t want to consider poorly designed and long outdated to be normal for Java. A normal date today would be `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 27 '20 at 17:51
  • 1
    Does this answer your question? [Convert calendar String to Calendar Object in java](https://stackoverflow.com/questions/43927047/convert-calendar-string-to-calendar-object-in-java) Or perhaps this? [How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?](https://stackoverflow.com/questions/44361292/how-can-i-convert-calendar-tostring-into-date-using-simpledateformat-parse) – Ole V.V. May 27 '20 at 17:53
  • What does a line in your CSV file look like? As I said, `GregorianCalendar` is poorly designed and long outdated, so going via that class seems to be an unpleasant detour? Can we avoid it? – Ole V.V. May 27 '20 at 17:57
  • Hi @OleV.V. thank you for your response / explanation. So I have columns for few simple transactions and other columns for their related disputes. Each transaction may have more than one dispute against it. Therefore I am having to loop through the disputes for every transaction to ensure I bring them all. If possible I would like to be adding extra rows as opposed to extra columns (each transaction should repeat up to 3 times if they have upto 3 disputes), however I am not too sure how complex that would get. I am going with the easier approach which is addng extra columns instead... – ERR May 27 '20 at 18:26

2 Answers2

1

You have two options to specify the date-time output format:

1- Using Java 8 Date and Time API classes under the java.time package (recommended)


         final GregorianCalendar gregorianCalendar = transaction.getDisputes().get(i).getReceivedDate();
        final String dateTimePattern = "yyyy-MM-dd HH:mm:ss.SSSZ";


        ZonedDateTime zonedDateTime = gregorianCalendar.toZonedDateTime();
        final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateTimePattern);
        final String dateFormat1 = dateFormatter.format(zonedDateTime);
        transactionData.add(dateFormat1);

2- Using the legacy Date-Time classes such as java.util.Date & java.text.SimpleDateFormat.

        final Date receivedDate = gregorianCalendar.getTime();
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimePattern);
        final String dateFormat2 = simpleDateFormat.format(receivedDate);
        transactionData.add(dateFormat2);

Ehab Qadah
  • 570
  • 4
  • 12
  • Hi @Ehbat Qadah this is very close to what I am looking for - this is now returning the format "Tue Apr 28 01:00:00 BST 2020". How to convert this now to timestamp with timezone? Thanks – ERR May 27 '20 at 11:42
  • @ERR can you provide an example how exactly do you wand the output format? – Ehab Qadah May 27 '20 at 11:51
  • Hi @Ehab Qadah I want it to be "yyyy-mm-dd hh:mi:ss+/-tz". If that's not possible at least "yyyy-mm-dd hh:mi:ss". Thank you. – ERR May 27 '20 at 12:41
  • @ERR i updated the answer i hope it solves your use case, if yes please accept the answer – Ehab Qadah May 27 '20 at 14:35
  • 1
    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. May 27 '20 at 17:48
  • 1
    Good point will update it! – Ehab Qadah May 27 '20 at 17:58
  • If you believe that you cannot avoid receiving a `GregorianCalendar` object, you may use its [`toZonedDateTime`](https://docs.oracle.com/javase/10/docs/api/java/util/GregorianCalendar.html#toZonedDateTime()) method (since Java 8) to convert to a modern type, which you may then format using a `DateTimeFormatter`. – Ole V.V. May 27 '20 at 18:34
0

GregorianCalendar extends Calendar that has a getTime():Date method : https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTime()

gervais.b
  • 2,294
  • 2
  • 22
  • 46