001/** 002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.runtime.manager.xbean.converter; 017 018import java.time.LocalDate; 019import java.time.LocalDateTime; 020import java.time.LocalTime; 021import java.time.ZoneId; 022import java.time.ZonedDateTime; 023 024import org.apache.xbean.propertyeditor.AbstractConverter; 025 026public class ZonedDateTimeConverter extends AbstractConverter { 027 028 private static final LocalTime NO_TIME = LocalTime.of(0, 0); 029 030 private static final LocalDate NO_DATE = LocalDate.of(1970, 1, 1); 031 032 private static final ZoneId UTC = ZoneId.of("UTC"); 033 034 public ZonedDateTimeConverter() { 035 super(ZonedDateTime.class); 036 } 037 038 @Override 039 protected Object toObjectImpl(final String data) { 040 if (data.isEmpty()) { 041 return null; 042 } 043 String text = data.replace('/', '-'); // sanitize date format 044 switch (text.length()) { 045 case 10: // YYYY-MM-dd 046 return ZonedDateTime.of(LocalDate.parse(text), NO_TIME, ZoneId.of("UTC")); 047 case 18: // HH:mm:ss.SSSSSSSSS 048 case 15: // HH:mm:ss.SSSSSS 049 case 12: // HH:mm:ss.SSS 050 case 8: // HH:mm:ss 051 case 5: // HH:mm 052 return ZonedDateTime.of(NO_DATE, LocalTime.parse(text), UTC); 053 case 29: // YYYY-MM-dd HH:mm:ss.SSSSSSSSS 054 case 26: // YYYY-MM-dd HH:mm:ss.SSSSSS 055 case 23: // YYYY-MM-dd HH:mm:ss.SSS 056 case 19: // YYYY-MM-dd HH:mm:ss 057 case 16: // YYYY-MM-dd HH:mm 058 default: // YYYY-MM-dd HH:mm.ss+HH:mm[...] 059 text = text.replace(' ', 'T'); 060 if (text.contains("+") || text.contains("[")) { 061 int keepAsIt = text.indexOf('+'); 062 if (keepAsIt < 0) { 063 keepAsIt = text.indexOf('['); 064 } 065 // ensure we don't loose / in the [] 066 text = text.substring(0, keepAsIt) + data.substring(keepAsIt); 067 return ZonedDateTime.parse(text); 068 } 069 final LocalDateTime dateTime = 070 LocalDateTime.parse(text.endsWith("Z") ? text.substring(0, text.length() - 1) : text); 071 return ZonedDateTime.of(dateTime.toLocalDate(), dateTime.toLocalTime(), UTC); 072 } 073 } 074}