Java tutorial
/* * Copyright (C) 2015 Sitexa Open Source Project * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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.sitexa.android.community.utils; import org.apache.commons.lang3.time.DateUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateUtil extends DateUtils { public final static String FORMAT_YEAR = "yyyy"; public final static String FORMAT_MONTH_DAY = "MMdd"; public final static String FORMAT_DATE = "yyyy-MM-dd"; public final static String FORMAT_TIME = "HH:mm"; public final static String FORMAT_MONTH_DAY_TIME = "MMdd hh:mm"; public final static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm"; public final static String FORMAT_DATE2_TIME = "yyyy-MM-dd HH:mm:ss"; public final static String FORMAT_DATE1_TIME = "yyyy/MM/dd HH:mm"; public final static String FORMAT_DATE_TIME_SECOND = "yyyy/MM/dd HH:mm:ss"; private static SimpleDateFormat sdf = new SimpleDateFormat(); /** * ??? * * @param format ?null""?"yyyy-MM-dd HH:MM" * @return */ public static String getCurrentTime(String format) { if (format == null || format.trim().equals("")) { sdf.applyPattern(FORMAT_DATE_TIME); } else { sdf.applyPattern(format); } return sdf.format(new Date()); } // date?String // formatType?yyyy-MM-dd HH:mm:ss//yyyyMMdd HHmmss // data Date public static String dateToString(Date data, String formatType) { if (data != null) return new SimpleDateFormat(formatType).format(data); else return ""; } // long?String // currentTime??long // formatType??string? public static String longToString(long currentTime, String formatType) { String strTime = ""; Date date = longToDate(currentTime, formatType);// long?Date strTime = dateToString(date, formatType); // date?String return strTime; } // string?date // strTime??stringformatType???yyyy-MM-dd HH:mm:ss//yyyyMMdd // HHmmss // strTime??formatType?? public static Date stringToDate(String strTime, String formatType) { SimpleDateFormat formatter = new SimpleDateFormat(formatType); Date date = null; try { date = formatter.parse(strTime); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } // long?Date // currentTime??long // formatType???yyyy-MM-dd HH:mm:ss//yyyyMMdd HHmmss public static Date longToDate(long currentTime, String formatType) { Date dateOld = new Date(currentTime); // ?longdate String sDateTime = dateToString(dateOld, formatType); // date?string Date date = stringToDate(sDateTime, formatType); // String?Date return date; } // string?long // strTime??String // formatType? // strTime?formatType?? public static long stringToLong(String strTime, String formatType) { Date date = stringToDate(strTime, formatType); // String?date if (date == null) { return 0; } else { long currentTime = dateToLong(date); // date?long return currentTime; } } // date?long // date??date public static long dateToLong(Date date) { return date.getTime(); } public static String getTime(Date date) { SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm"); return format.format(date); } public static String getHourAndMin(Date date) { SimpleDateFormat format = new SimpleDateFormat("HH:mm"); return format.format(date); } /** * ?? */ public static int getAgeByBirthday(Date birthday) { Calendar cal = Calendar.getInstance(); if (cal.before(birthday)) { throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!"); } int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH) + 1; int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); cal.setTime(birthday); int yearBirth = cal.get(Calendar.YEAR); int monthBirth = cal.get(Calendar.MONTH) + 1; int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) { if (monthNow == monthBirth) { // monthNow==monthBirth if (dayOfMonthNow < dayOfMonthBirth) { age--; } } else { // monthNow>monthBirth age--; } } return age; } public static String getPrettyTime(Date date) { Date currentDate = new Date(); Calendar current_cal = Calendar.getInstance(Locale.CHINA); Calendar date_cal = Calendar.getInstance(Locale.CHINA); if (date == null) { date = currentDate; } String time = getHourAndMin(date); current_cal.setTime(currentDate); date_cal.setTime(date); int current_year = current_cal.get(Calendar.YEAR); int current_monthOfYear = current_cal.get(Calendar.MONTH); int current_dayOfMonth = current_cal.get(Calendar.DAY_OF_MONTH); int current_hour = current_cal.get(Calendar.HOUR_OF_DAY); int current_minOfHour = current_cal.get(Calendar.MINUTE); int year = date_cal.get(Calendar.YEAR); int monthOfYear = date_cal.get(Calendar.MONTH); int dayOfMonth = date_cal.get(Calendar.DAY_OF_MONTH); int hourOfDay = date_cal.get(Calendar.HOUR_OF_DAY); int minOfHour = date_cal.get(Calendar.MINUTE); if (current_year > year || current_monthOfYear > monthOfYear) {//? ? return getTime(date); } else if (current_dayOfMonth > dayOfMonth) {//? int days = current_dayOfMonth - dayOfMonth; String result = ""; switch (days) { case 1: result = " " + time; break; case 2: result = "? " + time; break; default: result = days + "?"; break; } return result; } else if (current_hour > hourOfDay) {//?? return current_hour - hourOfDay + "??"; } else if (current_minOfHour > minOfHour) {//? return current_minOfHour - minOfHour + "?"; } else { return ""; } } }