Java tutorial
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main { public static SimpleDateFormat DAY = new SimpleDateFormat("M-d", Locale.ENGLISH); public static SimpleDateFormat DATE = new SimpleDateFormat("y-M-d", Locale.ENGLISH); public static String formatDuration(Date begDate, Date endDate) { // TODO Auto-generated method stub StringBuilder builder = new StringBuilder(formatDate(begDate)); builder.append(" - "); builder.append(formatDate(endDate)); return builder.toString(); } public static String formatDate(Date date) { // TODO Auto-generated method stub Calendar calender = Calendar.getInstance(); int thisyear = calender.get(Calendar.YEAR); calender.setTime(date); int dateyear = calender.get(Calendar.YEAR); if (thisyear == dateyear) { return DAY.format(date); } return DATE.format(date); } public static String format(String format, Date date) { return new SimpleDateFormat(format, Locale.ENGLISH).format(date); } }