Java Hour Format formatAddTime(Date addTime)

Here you can find the source of formatAddTime(Date addTime)

Description

format Add Time

License

Apache License

Declaration

public static String formatAddTime(Date addTime) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final DateFormat YEAR_MONTH_DAY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    public static final DateFormat MONTH_DAY_FORMAT = new SimpleDateFormat("MM-dd");
    public static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");

    public static String formatAddTime(Date addTime) {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH);
        int curDay = calendar.get(Calendar.DATE);

        calendar.setTime(addTime);//from  www  . jav  a  2 s. co m
        int addYear = calendar.get(Calendar.YEAR);
        int addMonth = calendar.get(Calendar.MONTH);
        int addDay = calendar.get(Calendar.DATE);

        if (addYear != curYear)
            return YEAR_MONTH_DAY_FORMAT.format(addTime);
        if (addMonth != curMonth || addDay != curDay)
            return MONTH_DAY_FORMAT.format(addTime);
        return TIME_FORMAT.format(addTime);
    }
}

Related

  1. format16ByDate(Date aDate)
  2. format1StringToformat2String(String dateStr)
  3. format2NormalTime(Date date)
  4. format_default(Date time)
  5. formatAbsoluteTime(long time)
  6. formatAllDate(Date date)
  7. formatAsCassandraDateTime(Date date)
  8. formatAsDateAndTime(Date date)
  9. formatAsHour(long l)