[Java]時間のフォーマット:昨日、今日、明日、曜日

5282 ワード

最近androidプロジェクトでは、今日、昨日、曜日のように、整理して修正したコードを、必要な人に共有するようにフォーマットされています.
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeTest {

	private static GregorianCalendar calendar = new GregorianCalendar();

	public TimeTest() {
	}

	//   “yyyy-mm-dd”            
	public static long getMillis(String dateString) {
		String[] date = dateString.split("-");
		return getMillis(date[0], date[1], date[2]);

	}

	//       、 、 ,          
	public static long getMillis(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month, day);
		return calendar.getTimeInMillis();

	}

	//       、 、 ,          
	public static long getMillis(String yearString, String monthString,
			String dayString) {
		int year = Integer.parseInt(yearString);
		int month = Integer.parseInt(monthString)-1;
		int day = Integer.parseInt(dayString);
		return getMillis(year, month, day);

	}

	//            
	public static long getNow() {
		GregorianCalendar now = new GregorianCalendar();
		return now.getTimeInMillis();

	}

	//         ,       
	public static String getDate(long millis) {
		calendar.setTimeInMillis(millis);
		return DateFormat.getDateInstance().format(calendar.getTime());

	}

	//         ,    
	public static int getYear(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.YEAR);

	}

	//         ,    
	public static int getMonth(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.MONTH);

	}

	//         ,    
	public static int getDay(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.DATE);

	}

	//         ,    
	public static int getHour(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.HOUR_OF_DAY);

	}

	//         ,    
	public static int getMinute(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.MINUTE);

	}

	//         ,   
	public static int getSecond(long millis) {
		calendar.setTimeInMillis(millis);
		return calendar.get(Calendar.SECOND);

	}
	//            
	public static String getWeek(long millis) {
		calendar.setTimeInMillis(millis);
		String week = "";
		int cweek = calendar.get(Calendar.DAY_OF_WEEK);
		switch (cweek) {
		case 1:
			week = " ";
			break;
		case 2:
			week = " ";
			break;
		case 3:
			week = " ";
			break;
		case 4:
			week = " ";
			break;
		case 5:
			week = " ";
			break;
		case 6:
			week = " ";
			break;
		case 7:
			week = " ";
			break;
		}
		return week;

	}

	//       
	public static String getTodayData() {
		return getDate(getNow());

	}

	//       
	public static String getTomoData() {
		// 86400000       
		return getDate(getNow() + 86400000);

	}

	//       
	public static String getTheDayData() {
		return getDate(getNow() + 86400000 + 86400000);
	}

	//       
	public static String getYesData() {
		return getDate(getNow() - 86400000L);
	}

	//       
	public static String getBeforeYesData() {
		return getDate(getNow() - 86400000L - 86400000L);
	}
	
	/**
	 *           
	 * @return
	 */
	public static String StringData() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mYear = String.valueOf(c.get(Calendar.YEAR)); //       
		String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);//       
		String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));//            
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = " ";
		} else if ("2".equals(mWay)) {
			mWay = " ";
		} else if ("3".equals(mWay)) {
			mWay = " ";
		} else if ("4".equals(mWay)) {
			mWay = " ";
		} else if ("5".equals(mWay)) {
			mWay = " ";
		} else if ("6".equals(mWay)) {
			mWay = " ";
		} else if ("7".equals(mWay)) {
			mWay = " ";
		}
		return mYear + " " + mMonth + " " + mDay + " " + "   " + mWay;
	}
	/**
	 *       、       
	 * @param date     20xx-xx-xx
	 * @return
	 */
	public static String getCustomStr(String date){
		if(getMillis(date)==getMillis(getBeforeYesData())){
			return "  ";
		}else if(getMillis(date)==getMillis(getYesData())){
			return "  ";
		}else if(getMillis(date)==getMillis(getTodayData())){
			return "  ";
		}else if(getMillis(date)==getMillis(getTomoData())){
			return "  ";
		}else if(getMillis(date)==getMillis(getTheDayData())){
			return "  ";
		}else{
			return "  "+getWeek((getMillis(date)));
		}
	}
	
	public static void main(String[] args) {
		System.out.println("  :" + getBeforeYesData());
		System.out.println("  :" + getYesData());
		System.out.println("  :" + getTodayData());
		System.out.println("  :" + getTomoData());
		System.out.println("  :" + getTheDayData());
		System.out.println(StringData());
		System.out.println("  " + getWeek((getMillis("2015-1-14"))));
		System.out.println(getCustomStr("2015-01-16"));
	}
}
質問があれば、伝言で答えてください.