Aandroid上的时间转换(将时间转换成各种格式)

没事又来分享点干货,在开发过程中时间的转换还是经常被用到的,话不多说直接上货;

@SuppressLint("SimpleDateFormat")
public class TimeUtils {
	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat(
			"HH:mm:ss");
	public static final SimpleDateFormat DEFAULT_MIN_FORMAT = new SimpleDateFormat(
			"mm:ss");
	public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat(
			"yyyy/MM/dd");
	public static final SimpleDateFormat DEL_FORMAT_DATE = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static final SimpleDateFormat INT_HOUR_FORMAT = new SimpleDateFormat(
			"HH");

	private TimeUtils() {
		throw new AssertionError();
	}

	/**
	 * long time to int
	 * 
	 * @param timeInMillis
	 * @param dateFormat
	 * @return
	 */
	public static int getCurrentHourInInt(long timeInMillis,
			SimpleDateFormat dateFormat) {
		String date = dateFormat.format(new Date(timeInMillis));
		int time = 0;
		if (!TextUtils.isEmpty(date)) {
			time = Integer.parseInt(date);
		}
		return time;
	}

	/**
	 * long time to string
	 * 
	 * @param timeInMillis
	 * @param dateFormat
	 * @return
	 */
	public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
		return dateFormat.format(new Date(timeInMillis));
	}

	/**
	 * long time to int
	 * 
	 * @param timeInMillis
	 * @param dateFormat
	 * @return
	 */
	public static long getCurrentDateInLong(long timeInMillis,
			SimpleDateFormat dateFormat) {
		String date = dateFormat.format(new Date(timeInMillis));
		long time = 0;
		if (!TextUtils.isEmpty(date)) {
			time = Long.parseLong(date);
		}
		return time;
	}

	/**
	 * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
	 * 
	 * @param timeInMillis
	 * @return
	 */
	public static String getTime(long timeInMillis) {
		return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
	}

	/**
	 * get current time in milliseconds
	 * 
	 * @return
	 */
	public static long getCurrentTimeInLong() {
		return System.currentTimeMillis();
	}

	/**
	 * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
	 * 
	 * @return
	 */
	public static String getCurrentTimeInString() {
		return getTime(getCurrentTimeInLong());
	}

	/**
	 * get current time in milliseconds
	 * 
	 * @return
	 */
	public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
		return getTime(getCurrentTimeInLong(), dateFormat);
	}

福利分享:
nginx下载:https://download.csdn.net/download/qq_35350654/10707741

有效地址遍历软件:https://download.csdn.net/download/qq_35350654/10637263

END 

编辑:吴明辉

没有更多推荐了,返回首页