Java tutorial
/* * Copyright 2014-2018 NTT Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package jp.co.ntt.atrs.domain.common.util; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.LocalTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.util.Date; /** * ?? * * @author NTT */ public class DateTimeUtil { /** * ()??? */ private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern("yyyy/MM/dd"); /** * ()??? */ private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormat.forPattern("HHmm"); /** * */ private DateTimeUtil() { // ??? } /** * DateTime??? * * @param date * @param timeString (HHmm) * @return ????????DateTime */ public static DateTime toDateTime(Date date, String timeString) { return new LocalDate(date).toDateTime(DateTimeUtil.toLocalTime(timeString)); } /** * DateTime??? * * @param dateString (yyyy/MM/dd) * @return ??????DateTime */ public static DateTime toDateTime(String dateString) { return DATE_FORMATTER.parseDateTime(dateString); } /** * LocalTime??? * * @param timeString (HHmm) * @return ??????LocalTime */ public static LocalTime toLocalTime(String timeString) { return TIME_FORMATTER.parseLocalTime(timeString); } /** * (yyyy/MM/dd)??? * * @param date * @return (yyyy/MM/dd) */ public static String toFormatDateString(Date date) { if (date == null) { return ""; } return new DateTime(date).toString("yyyy/MM/dd"); } /** * (yyyy/MM/dd)??? * * @param dateTime DateTime * @return (yyyy/MM/dd) */ public static String toFormatDateString(DateTime dateTime) { if (dateTime == null) { return ""; } return dateTime.toString("yyyy/MM/dd"); } /** * (HH:mm)??? * * @param timeString (HHmm) * @return (HH:mm) */ public static String toFormatTimeString(String timeString) { String result = timeString; if (timeString != null && 2 < timeString.length()) { result = String.format("%s:%s", timeString.substring(0, 2), timeString.substring(2)); } return result; } }