Author |
Message |
13/04/2021 09:39:35
|
mmohr
Active
Joined: 13/04/2021 08:22:08
Messages: 7
Offline
|
Hello,
I want to use a Label to display a timestamp that I receive from ldap. The timestamp is stored in the "GeneralizedTime" format, which I then pass as value for the Label.
An example of a timestamp: "20080508200557Z" should result in: 2008 May 8, 20:05:57. But in my testing it results in: 2606 April 30, 06:56:40.
Is the "GeneralizedTime" format supported at all?
|
|
|
13/04/2021 09:43:19
|
Admin
Power User
Joined: 21/11/2007 11:36:10
Messages: 38
Offline
|
Hi,
no LABEL (if used with FORMAT="date"), FIELD, FORMATTEDFIELD, CALENDARFIELD either use "java.util.Date" or its long-representation, or LocalDate.
Please convert to one of these formats.
If using LocalDate/LocalDayTime - then pass TIMEZONE with value "LOCAL". If using java.util.Date: pass the TIMEZONE value properly...!
Regards, Björn
|
|
|
13/04/2021 13:12:56
|
mmohr
Active
Joined: 13/04/2021 08:22:08
Messages: 7
Offline
|
Admin wrote:
Hi,
no LABEL (if used with FORMAT="date"), FIELD, FORMATTEDFIELD, CALENDARFIELD either use "java.util.Date" or its long-representation, or LocalDate.
Please convert to one of these formats.
I tried displaying an epoch timestamp of some minutes ago, but that still displays a wrong time.
What I tried:
Code:
LABELNode label = new LABELNode();
label.setText("1618316807");
label.setFormat("datetime");
label.setTimezone(TimeZone.getDefault().getID());
Should show:04/13/2021 14:26:47
Instead shows: 01/19/1970 17:31:56
Do you have any idea what I'm doing wrong?
|
|
|
13/04/2021 14:02:23
|
CaptainCasa
Power User
Joined: 21/11/2007 12:23:06
Messages: 5535
Offline
|
You value seems a bit "low"... ;-)
...you need to do it the following way:
Code:
LocalDateTime ld = LocalDateTime.of(y,m,d,...); // parse your value before...
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR,ld.getYear());
cal.set(Calendar.MONTH,ld.getMonthValue()-1);
cal.set(Calendar.DAY_OF_MONTH,ld.getDayOfMonth());
cal.set(Calendar.HOUR_OF_DAY,ld.getHour());
cal.set(Calendar.MINUTE,ld.getMinute());
cal.set(Calendar.SECOND,ld.getSecond());
cal.set(Calendar.MILLISECOND,ld.getNano() / 1000000);
String dateAsLongString = ""+cal.getTime().getTime();
LABELNode label = new LABELNode();
label.setText(dateAsLongString);
label.setFormat("datetime");
label.setTimezone(TimeZone.getDefault().getID());
The "long-String" that you pass as TEXT-attribute needs to represent milliseconds since 1st of January 1970... ;-)
(Is there an easier way to convert LocalDate into a long than going through the Calendar? I am not sure...)
OK?
Kind regards! Björn
|
Björn Müller, CaptainCasa GmbH |
|
|
|