-1

Does anyone know how to grab or create the cdate value from Date? I haven't been able to figure out how to do so and I don't know how to necessarily get it. In the Date class it is there as private transient BaseCalendar.Date cdate; I don't see an option to grabt the field from the Date object itself.

I am setting this date using request.setGatewayEndTime(Calendar.getInstance().getTime()); where the calendar is from the java.util.Calendar import

debug pic

5
  • 7
    This smells like an XY Problem. Why do you think you need the cdate field? That is an implementation detail that is intentionally not exposed to the end user. What kind of information do you hope to get from that? Generally speaking, you should try to avoid using java.util.Data and java.util.Calendar whenever you can and use the modern java.time API instead. Commented Jul 22, 2022 at 18:03
  • If you want the string value shown in the debugger, use a SimpleDateFormat (or, as suggested, DateTimeFormatter) Commented Jul 22, 2022 at 18:08
  • @JoachimSauer oh thanks for the feed back I didn't know that as I am just playing around with java Commented Jul 22, 2022 at 18:20
  • 1
    It looks like what you actually want to do (the X of your XY problem) is format a Date object in ISO-8601 format. Commented Jul 22, 2022 at 18:24
  • I strongly recommend you don’t use Date and Calendar. Those classes are poorly designed and long outdated. Instead depending on your exact needs use for example OffsetDateTime from java.time, the modern Java date and time API. Commented Jul 23, 2022 at 8:13

1 Answer 1

1

As commented, trying to access internally defined fields that were intentionally hidden from outside calling programmers would be unwise.

As commented, you seem to have an XY Problem. I'll take a stab at what you might need.

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use Calendar, Date, etc.

You claim to have a call to a method requiring a java.util.Date object that represents the current moment as seen in UTC, that is, with an offset of zero hours-minutes-seconds. To capture the current moment, use Instant class.

Instant now = Instant.now() ;  // Current moment as seen with an offset of zero.

I suggest you educate the author of that called method about how Date has been replaced by Instant.

Until they update their API, you can easily convert between legacy and modern classes by calling conversion methods added to the old classes.

java.util.Date d = Date.from( now ) ;
request.setGatewayEndTime( d );

As for the BaseCalendar.Date field, if your goal is to get a string in ISO 8601 format similar to the string you show in your screenshot which is nearly in standard format but omits the COLON from between the hours and minutes of the offset, then simply call Instant#toSting. The java.time classes use ISO 8601 format by default when parsing/generating strings.

String output = now.toString() ;  // Generate text in standard ISO 8601 format.

Your screenshot uses another offset other than zero. If you wish to see a moment with the offset of a particular time zone, apply a ZoneID object to produce a ZonedDateTime object.

ZoneId z = ZoneId.of( "America/La_Paz" ) ;
ZonedDateTime zdt = now.atZone( z ) ;

Produce text in standard ISO 8601 format that has been wisely extended to include the name of the time zone in square brackets.

String output = zdt.toString() ;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.