// Copyright (c) 2021, Fr.Terrot. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package io.gitea.model; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.OffsetDateTime; import java.util.Date; /** Handles Gitea to Mylyn date conversions */ public class GiteaDateTimeUtils { private static final String dateFormat_from_LocalDateTime = "yyyy-MM-dd'T'HH:mm:ss"; //$NON-NLS-1$ private static final String dateFormat_from_Offset = "yyyy-MM-dd'T'HH:mm:ssX"; //$NON-NLS-1$ private static final String dateFormat_1 = "yyyy-MM-dd HH:mm:ss"; //$NON-NLS-1$ private static final String dateFormat_2 = "yyyy-MM-dd HH:mm"; //$NON-NLS-1$ private static final String dateFormat_3 = "yyyy-MM-dd"; //$NON-NLS-1$ private static final String dateFormat_1_TimeZone = "yyyy-MM-dd HH:mm:ss Z"; //$NON-NLS-1$ private static final String dateFormat_2_TimeZone = "yyyy-MM-dd HH:mm z"; //$NON-NLS-1$ private static final String dateFormat_3_TimeZone = "yyyy-MM-dd z"; //$NON-NLS-1$ // Order is significant private static final String[] dateFormats = { dateFormat_from_Offset, dateFormat_from_LocalDateTime, dateFormat_1_TimeZone, dateFormat_1, dateFormat_2_TimeZone, dateFormat_2, dateFormat_3_TimeZone, dateFormat_3 }; /** * Note: Date formatter constructed within method for thread safety */ public static final Date parseDate(String dateString) { for (String format : dateFormats) { try { SimpleDateFormat simpleFormatter = new SimpleDateFormat(format); return simpleFormatter.parse(dateString); } catch (ParseException e) { } catch (NumberFormatException e) { } } return null; } /** Convert OffsetDateTime to Date */ public static final Date toDate(OffsetDateTime offset) { return parseDate(offset.toString()); // FIXME: use better Offset to date convertion } }