Back to project page android-utils.
The source code is released under:
Apache License
If you think the Android project android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2014 Zhenguo Jin//from w w w . j ava2 s. c om * * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.worthed.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * ????? * * @author jingle1267@163.com */ public class DateUtils { /** * ???? * */ public static final String yyyyMMDD = "yyyy-MM-dd"; public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss"; public static final String HHmmss = "HH:mm:ss"; public static final String hhmmss = "HH:mm:ss"; public static final String LOCALE_DATE_FORMAT = "yyyy?M?d? HH:mm:ss"; public static final String DB_DATA_FORMAT = "yyyy-MM-DD HH:mm:ss"; public static final String NEWS_ITEM_DATE_FORMAT = "hh:mm M?d? yyyy"; public static String dateToString(Date date, String pattern) throws Exception { return new SimpleDateFormat(pattern).format(date); } public static Date stringToDate(String dateStr, String pattern) throws Exception { return new SimpleDateFormat(pattern).parse(dateStr); } /** * ?Date???????????? * * @param date Date?? * @param type ????????? * @return ????????????? */ public static String formatDate(Date date, String type) { try { SimpleDateFormat df = new SimpleDateFormat(type); return df.format(date); } catch (Exception e) { e.printStackTrace(); } return null; } /** * ???????????Date?? * * @param dateStr ????? * @param type ???????? * @return Date?? */ public static Date parseDate(String dateStr, String type) { SimpleDateFormat df = new SimpleDateFormat(type); Date date = null; try { date = df.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * ??? * * @param date Date?? * @return ? */ public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /** * ??? * * @param date Date?? * @return ? */ public static int getMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MONTH) + 1; } /** * ??? * * @param date Date?? * @return ? */ public static int getDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_MONTH); } /** * ?????? ???????, ??, ???, XXXX-XX-XX, ... * * @param time ?? * @return ????????????????????? */ public static String translateDate(Long time) { long oneDay = 24 * 60 * 60 * 1000; Calendar current = Calendar.getInstance(); Calendar today = Calendar.getInstance(); //?? today.set(Calendar.YEAR, current.get(Calendar.YEAR)); today.set(Calendar.MONTH, current.get(Calendar.MONTH)); today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH)); // Calendar.HOUR12????????? Calendar.HOUR_OF_DAY24????????? today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); long todayStartTime = today.getTimeInMillis(); if (time >= todayStartTime && time < todayStartTime + oneDay) { // today return "??"; } else if (time >= todayStartTime - oneDay && time < todayStartTime) { // yesterday return "??"; } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) { // the day before yesterday return "???"; } else if (time > todayStartTime + oneDay) { // future return "????????"; } else { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(time); return dateFormat.format(date); } } }