Here you can find the source of formatAddTime(Date addTime)
public static String formatAddTime(Date addTime)
//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); } }