skip to Main Content

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”?

2

Answers


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

    Login or Signup to reply.
  2. 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);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search