com.kccomy.orgar.util.DateFormatUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.kccomy.orgar.util.DateFormatUtil.java

Source

/*
 * Copyright (C) 2016. kccomy, Orgar
 *
 * 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 com.kccomy.orgar.util;

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

public class DateFormatUtil {

    private static final DateTimeFormatter TIME = DateTimeFormat.forPattern("a hh:mm");
    private static final DateTimeFormatter WEEK = DateTimeFormat.forPattern("E");
    private static final DateTimeFormatter DATE = DateTimeFormat.forPattern("yy/MM/dd");

    public static String getFormattedDate(Date date) {
        LocalDateTime modified = LocalDateTime.fromDateFields(date);
        LocalDateTime now = LocalDateTime.now();

        if (modified.getWeekOfWeekyear() == now.getWeekOfWeekyear() && modified.getYear() == now.getYear()) {

            if (modified.getDayOfWeek() == now.getDayOfWeek()) {
                return modified.toString(TIME);
            } else {
                return modified.toString(WEEK);
            }
        } else {
            return modified.toString(DATE);
        }
    }
}