123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725 |
- package com.cooleshow.base.utils;
- import android.annotation.SuppressLint;
- import android.os.Build;
- import android.provider.Settings;
- import com.cooleshow.base.constanst.TimeConstants;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.HashMap;
- import java.util.Locale;
- import java.util.Map;
- import androidx.annotation.NonNull;
- /**
- * <pre>
- * author: Blankj
- * blog : http://blankj.com
- * time : 2016/08/02
- * desc : utils about time
- * </pre>
- */
- public final class TimeUtils {
- private static final ThreadLocal<Map<String, SimpleDateFormat>> SDF_THREAD_LOCAL
- = new ThreadLocal<Map<String, SimpleDateFormat>>() {
- @Override
- protected Map<String, SimpleDateFormat> initialValue() {
- return new HashMap<>();
- }
- };
- private static SimpleDateFormat getDefaultFormat() {
- return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
- }
- public static SimpleDateFormat getSafeDateFormatForDay() {
- return getSafeDateFormat("yyyy-MM-dd");
- }
- public static SimpleDateFormat getSafeDateFormatForMinutes() {
- return getSafeDateFormat("yyyy-MM-dd HH:mm");
- }
- /**
- * Checks whether the device is using Network Provided Time or not.
- * Useful in situations where you want to verify that the device has a correct time set, to avoid fraud, or if you want to prevent the user from messing with the time and abusing your "one-time" and "expiring" features.
- *
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isUsingNetworkProvidedTime() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- return Settings.Global.getInt(Utils.getApp().getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
- } else {
- return android.provider.Settings.System.getInt(Utils.getApp().getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
- }
- }
- @SuppressLint("SimpleDateFormat")
- public static SimpleDateFormat getSafeDateFormat(String pattern) {
- Map<String, SimpleDateFormat> sdfMap = SDF_THREAD_LOCAL.get();
- //noinspection ConstantConditions
- SimpleDateFormat simpleDateFormat = sdfMap.get(pattern);
- if (simpleDateFormat == null) {
- simpleDateFormat = new SimpleDateFormat(pattern);
- sdfMap.put(pattern, simpleDateFormat);
- }
- return simpleDateFormat;
- }
- private TimeUtils() {
- throw new UnsupportedOperationException("u can't instantiate me...");
- }
- /**
- * Milliseconds to the formatted time string.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param millis The milliseconds.
- * @return the formatted time string
- */
- public static String millis2String(final long millis) {
- return millis2String(millis, getDefaultFormat());
- }
- /**
- * Milliseconds to the formatted time string.
- *
- * @param millis The milliseconds.
- * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
- * @return the formatted time string
- */
- public static String millis2String(long millis, @NonNull final String pattern) {
- return millis2String(millis, getSafeDateFormat(pattern));
- }
- /**
- * Milliseconds to the formatted time string.
- *
- * @param millis The milliseconds.
- * @param format The format.
- * @return the formatted time string
- */
- public static String millis2String(final long millis, @NonNull final DateFormat format) {
- return format.format(new Date(millis));
- }
- /**
- * Formatted time string to the milliseconds.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the milliseconds
- */
- public static long string2Millis(final String time) {
- return string2Millis(time, getDefaultFormat());
- }
- /**
- * Formatted time string to the milliseconds.
- *
- * @param time The formatted time string.
- * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
- * @return the milliseconds
- */
- public static long string2Millis(final String time, @NonNull final String pattern) {
- return string2Millis(time, getSafeDateFormat(pattern));
- }
- /**
- * Formatted time string to the milliseconds.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the milliseconds
- */
- public static long string2Millis(final String time, @NonNull final DateFormat format) {
- try {
- return format.parse(time).getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return -1;
- }
- /**
- * Formatted time string to the date.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the date
- */
- public static Date string2Date(final String time) {
- return string2Date(time, getDefaultFormat());
- }
- /**
- * Formatted time string to the date.
- *
- * @param time The formatted time string.
- * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
- * @return the date
- */
- public static Date string2Date(final String time, @NonNull final String pattern) {
- return string2Date(time, getSafeDateFormat(pattern));
- }
- /**
- * Formatted time string to the date.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the date
- */
- public static Date string2Date(final String time, @NonNull final DateFormat format) {
- try {
- return format.parse(time);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Date to the formatted time string.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param date The date.
- * @return the formatted time string
- */
- public static String date2String(final Date date) {
- return date2String(date, getDefaultFormat());
- }
- /**
- * Date to the formatted time string.
- *
- * @param date The date.
- * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
- * @return the formatted time string
- */
- public static String date2String(final Date date, @NonNull final String pattern) {
- return getSafeDateFormat(pattern).format(date);
- }
- /**
- * Date to the formatted time string.
- *
- * @param date The date.
- * @param format The format.
- * @return the formatted time string
- */
- public static String date2String(final Date date, @NonNull final DateFormat format) {
- try {
- return format.format(date);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * Date to the milliseconds.
- *
- * @param date The date.
- * @return the milliseconds
- */
- public static long date2Millis(final Date date) {
- return date.getTime();
- }
- /**
- * Milliseconds to the date.
- *
- * @param millis The milliseconds.
- * @return the date
- */
- public static Date millis2Date(final long millis) {
- return new Date(millis);
- }
- /**
- * Return the time span, in unit.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time1 The first formatted time string.
- * @param time2 The second formatted time string.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span, in unit
- */
- public static long getTimeSpan(final String time1,
- final String time2,
- @TimeConstants.Unit final int unit) {
- return getTimeSpan(time1, time2, getDefaultFormat(), unit);
- }
- /**
- * Return the time span, in unit.
- *
- * @param time1 The first formatted time string.
- * @param time2 The second formatted time string.
- * @param format The format.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span, in unit
- */
- public static long getTimeSpan(final String time1,
- final String time2,
- @NonNull final DateFormat format,
- @TimeConstants.Unit final int unit) {
- return millis2TimeSpan(string2Millis(time1, format) - string2Millis(time2, format), unit);
- }
- /**
- * Return the time span, in unit.
- *
- * @param date1 The first date.
- * @param date2 The second date.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span, in unit
- */
- public static long getTimeSpan(final Date date1,
- final Date date2,
- @TimeConstants.Unit final int unit) {
- return millis2TimeSpan(date2Millis(date1) - date2Millis(date2), unit);
- }
- /**
- * Return the time span, in unit.
- *
- * @param millis1 The first milliseconds.
- * @param millis2 The second milliseconds.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span, in unit
- */
- public static long getTimeSpan(final long millis1,
- final long millis2,
- @TimeConstants.Unit final int unit) {
- return millis2TimeSpan(millis1 - millis2, unit);
- }
- /**
- * Return the fit time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time1 The first formatted time string.
- * @param time2 The second formatted time string.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0, return null</li>
- * <li>precision = 1, return 天</li>
- * <li>precision = 2, return 天, 小时</li>
- * <li>precision = 3, return 天, 小时, 分钟</li>
- * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
- * <li>precision >= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
- * </ul>
- * @return the fit time span
- */
- public static String getFitTimeSpan(final String time1,
- final String time2,
- final int precision) {
- long delta = string2Millis(time1, getDefaultFormat()) - string2Millis(time2, getDefaultFormat());
- return millis2FitTimeSpan(delta, precision);
- }
- /**
- * Return the fit time span.
- *
- * @param time1 The first formatted time string.
- * @param time2 The second formatted time string.
- * @param format The format.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0, return null</li>
- * <li>precision = 1, return 天</li>
- * <li>precision = 2, return 天, 小时</li>
- * <li>precision = 3, return 天, 小时, 分钟</li>
- * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
- * <li>precision >= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
- * </ul>
- * @return the fit time span
- */
- public static String getFitTimeSpan(final String time1,
- final String time2,
- @NonNull final DateFormat format,
- final int precision) {
- long delta = string2Millis(time1, format) - string2Millis(time2, format);
- return millis2FitTimeSpan(delta, precision);
- }
- /**
- * Return the fit time span.
- *
- * @param date1 The first date.
- * @param date2 The second date.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0, return null</li>
- * <li>precision = 1, return 天</li>
- * <li>precision = 2, return 天, 小时</li>
- * <li>precision = 3, return 天, 小时, 分钟</li>
- * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
- * <li>precision >= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
- * </ul>
- * @return the fit time span
- */
- public static String getFitTimeSpan(final Date date1, final Date date2, final int precision) {
- return millis2FitTimeSpan(date2Millis(date1) - date2Millis(date2), precision);
- }
- /**
- * Return the fit time span.
- *
- * @param millis1 The first milliseconds.
- * @param millis2 The second milliseconds.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0, return null</li>
- * <li>precision = 1, return 天</li>
- * <li>precision = 2, return 天, 小时</li>
- * <li>precision = 3, return 天, 小时, 分钟</li>
- * <li>precision = 4, return 天, 小时, 分钟, 秒</li>
- * <li>precision >= 5,return 天, 小时, 分钟, 秒, 毫秒</li>
- * </ul>
- * @return the fit time span
- */
- public static String getFitTimeSpan(final long millis1,
- final long millis2,
- final int precision) {
- return millis2FitTimeSpan(millis1 - millis2, precision);
- }
- /**
- * Return the current time in milliseconds.
- *
- * @return the current time in milliseconds
- */
- public static long getNowMills() {
- return System.currentTimeMillis();
- }
- /**
- * Return the current formatted time string.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @return the current formatted time string
- */
- public static String getNowString() {
- return millis2String(System.currentTimeMillis(), getDefaultFormat());
- }
- /**
- * Return the current formatted time string.
- *
- * @param format The format.
- * @return the current formatted time string
- */
- public static String getNowString(@NonNull final DateFormat format) {
- return millis2String(System.currentTimeMillis(), format);
- }
- /**
- * Return the current date.
- *
- * @return the current date
- */
- public static Date getNowDate() {
- return new Date();
- }
- /**
- * Return the time span by now, in unit.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span by now, in unit
- */
- public static long getTimeSpanByNow(final String time, @TimeConstants.Unit final int unit) {
- return getTimeSpan(time, getNowString(), getDefaultFormat(), unit);
- }
- /**
- * Return the time span by now, in unit.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span by now, in unit
- */
- public static long getTimeSpanByNow(final String time,
- @NonNull final DateFormat format,
- @TimeConstants.Unit final int unit) {
- return getTimeSpan(time, getNowString(format), format, unit);
- }
- /**
- * Return the time span by now, in unit.
- *
- * @param date The date.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span by now, in unit
- */
- public static long getTimeSpanByNow(final Date date, @TimeConstants.Unit final int unit) {
- return getTimeSpan(date, new Date(), unit);
- }
- /**
- * Return the time span by now, in unit.
- *
- * @param millis The milliseconds.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the time span by now, in unit
- */
- public static long getTimeSpanByNow(final long millis, @TimeConstants.Unit final int unit) {
- return getTimeSpan(millis, System.currentTimeMillis(), unit);
- }
- /**
- * Return the fit time span by now.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0,返回 null</li>
- * <li>precision = 1,返回天</li>
- * <li>precision = 2,返回天和小时</li>
- * <li>precision = 3,返回天、小时和分钟</li>
- * <li>precision = 4,返回天、小时、分钟和秒</li>
- * <li>precision >= 5,返回天、小时、分钟、秒和毫秒</li>
- * </ul>
- * @return the fit time span by now
- */
- public static String getFitTimeSpanByNow(final String time, final int precision) {
- return getFitTimeSpan(time, getNowString(), getDefaultFormat(), precision);
- }
- /**
- * Return the fit time span by now.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0,返回 null</li>
- * <li>precision = 1,返回天</li>
- * <li>precision = 2,返回天和小时</li>
- * <li>precision = 3,返回天、小时和分钟</li>
- * <li>precision = 4,返回天、小时、分钟和秒</li>
- * <li>precision >= 5,返回天、小时、分钟、秒和毫秒</li>
- * </ul>
- * @return the fit time span by now
- */
- public static String getFitTimeSpanByNow(final String time,
- @NonNull final DateFormat format,
- final int precision) {
- return getFitTimeSpan(time, getNowString(format), format, precision);
- }
- /**
- * Return the fit time span by now.
- *
- * @param date The date.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0,返回 null</li>
- * <li>precision = 1,返回天</li>
- * <li>precision = 2,返回天和小时</li>
- * <li>precision = 3,返回天、小时和分钟</li>
- * <li>precision = 4,返回天、小时、分钟和秒</li>
- * <li>precision >= 5,返回天、小时、分钟、秒和毫秒</li>
- * </ul>
- * @return the fit time span by now
- */
- public static String getFitTimeSpanByNow(final Date date, final int precision) {
- return getFitTimeSpan(date, getNowDate(), precision);
- }
- /**
- * Return the fit time span by now.
- *
- * @param millis The milliseconds.
- * @param precision The precision of time span.
- * <ul>
- * <li>precision = 0,返回 null</li>
- * <li>precision = 1,返回天</li>
- * <li>precision = 2,返回天和小时</li>
- * <li>precision = 3,返回天、小时和分钟</li>
- * <li>precision = 4,返回天、小时、分钟和秒</li>
- * <li>precision >= 5,返回天、小时、分钟、秒和毫秒</li>
- * </ul>
- * @return the fit time span by now
- */
- public static String getFitTimeSpanByNow(final long millis, final int precision) {
- return getFitTimeSpan(millis, System.currentTimeMillis(), precision);
- }
- /**
- * Return the friendly time span by now.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the friendly time span by now
- * <ul>
- * <li>如果小于 1 秒钟内,显示刚刚</li>
- * <li>如果在 1 分钟内,显示 XXX秒前</li>
- * <li>如果在 1 小时内,显示 XXX分钟前</li>
- * <li>如果在 1 小时外的今天内,显示今天15:32</li>
- * <li>如果是昨天的,显示昨天15:32</li>
- * <li>其余显示,2016-10-15</li>
- * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
- * </ul>
- */
- public static String getFriendlyTimeSpanByNow(final String time) {
- return getFriendlyTimeSpanByNow(time, getDefaultFormat());
- }
- /**
- * Return the friendly time span by now.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the friendly time span by now
- * <ul>
- * <li>如果小于 1 秒钟内,显示刚刚</li>
- * <li>如果在 1 分钟内,显示 XXX秒前</li>
- * <li>如果在 1 小时内,显示 XXX分钟前</li>
- * <li>如果在 1 小时外的今天内,显示今天15:32</li>
- * <li>如果是昨天的,显示昨天15:32</li>
- * <li>其余显示,2016-10-15</li>
- * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
- * </ul>
- */
- public static String getFriendlyTimeSpanByNow(final String time,
- @NonNull final DateFormat format) {
- return getFriendlyTimeSpanByNow(string2Millis(time, format));
- }
- /**
- * Return the friendly time span by now.
- *
- * @param date The date.
- * @return the friendly time span by now
- * <ul>
- * <li>如果小于 1 秒钟内,显示刚刚</li>
- * <li>如果在 1 分钟内,显示 XXX秒前</li>
- * <li>如果在 1 小时内,显示 XXX分钟前</li>
- * <li>如果在 1 小时外的今天内,显示今天15:32</li>
- * <li>如果是昨天的,显示昨天15:32</li>
- * <li>其余显示,2016-10-15</li>
- * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
- * </ul>
- */
- public static String getFriendlyTimeSpanByNow(final Date date) {
- return getFriendlyTimeSpanByNow(date.getTime());
- }
- /**
- * Return the friendly time span by now.
- *
- * @param millis The milliseconds.
- * @return the friendly time span by now
- * <ul>
- * <li>如果小于 1 秒钟内,显示刚刚</li>
- * <li>如果在 1 分钟内,显示 XXX秒前</li>
- * <li>如果在 1 小时内,显示 XXX分钟前</li>
- * <li>如果在 1 小时外的今天内,显示今天15:32</li>
- * <li>如果是昨天的,显示昨天15:32</li>
- * <li>其余显示,2016-10-15</li>
- * <li>时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007</li>
- * </ul>
- */
- public static String getFriendlyTimeSpanByNow(final long millis) {
- long now = System.currentTimeMillis();
- long span = now - millis;
- if (span < 0)
- // U can read http://www.apihome.cn/api/java/Formatter.html to understand it.
- return String.format("%tc", millis);
- if (span < 1000) {
- return "刚刚";
- } else if (span < TimeConstants.MIN) {
- return String.format(Locale.getDefault(), "%d秒前", span / TimeConstants.SEC);
- } else if (span < TimeConstants.HOUR) {
- return String.format(Locale.getDefault(), "%d分钟前", span / TimeConstants.MIN);
- }
- // 获取当天 00:00
- long wee = getWeeOfToday();
- if (millis >= wee) {
- return String.format("今天%tR", millis);
- } else if (millis >= wee - TimeConstants.DAY) {
- return String.format("昨天%tR", millis);
- } else {
- return String.format("%tF", millis);
- }
- }
- private static long getWeeOfToday() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTimeInMillis();
- }
- /**
- * Return the milliseconds differ time span.
- *
- * @param millis The milliseconds.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the milliseconds differ time span
- */
- public static long getMillis(final long millis,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis + timeSpan2Millis(timeSpan, unit);
- }
- /**
- * Return the milliseconds differ time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the milliseconds differ time span
- */
- public static long getMillis(final String time,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return getMillis(time, getDefaultFormat(), timeSpan, unit);
- }
- /**
- * Return the milliseconds differ time span.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the milliseconds differ time span.
- */
- public static long getMillis(final String time,
- @NonNull final DateFormat format,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return string2Millis(time, format) + timeSpan2Millis(timeSpan, unit);
- }
- /**
- * Return the milliseconds differ time span.
- *
- * @param date The date.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the milliseconds differ time span.
- */
- public static long getMillis(final Date date,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return date2Millis(date) + timeSpan2Millis(timeSpan, unit);
- }
- /**
- * Return the formatted time string differ time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param millis The milliseconds.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final long millis,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return getString(millis, getDefaultFormat(), timeSpan, unit);
- }
- /**
- * Return the formatted time string differ time span.
- *
- * @param millis The milliseconds.
- * @param format The format.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final long millis,
- @NonNull final DateFormat format,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2String(millis + timeSpan2Millis(timeSpan, unit), format);
- }
- /**
- * Return the formatted time string differ time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final String time,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return getString(time, getDefaultFormat(), timeSpan, unit);
- }
- /**
- * Return the formatted time string differ time span.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final String time,
- @NonNull final DateFormat format,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2String(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit), format);
- }
- /**
- * Return the formatted time string differ time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param date The date.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final Date date,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return getString(date, getDefaultFormat(), timeSpan, unit);
- }
- /**
- * Return the formatted time string differ time span.
- *
- * @param date The date.
- * @param format The format.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span
- */
- public static String getString(final Date date,
- @NonNull final DateFormat format,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2String(date2Millis(date) + timeSpan2Millis(timeSpan, unit), format);
- }
- /**
- * Return the date differ time span.
- *
- * @param millis The milliseconds.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the date differ time span
- */
- public static Date getDate(final long millis,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2Date(millis + timeSpan2Millis(timeSpan, unit));
- }
- /**
- * Return the date differ time span.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the date differ time span
- */
- public static Date getDate(final String time,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return getDate(time, getDefaultFormat(), timeSpan, unit);
- }
- public static Date getDate(String time) {
- try {
- Date parse = getDefaultFormat().parse(time);
- return parse;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return getNowDate();
- }
- /**
- * Return the date differ time span.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the date differ time span
- */
- public static Date getDate(final String time,
- @NonNull final DateFormat format,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2Date(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit));
- }
- /**
- * Return the date differ time span.
- *
- * @param date The date.
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the date differ time span
- */
- public static Date getDate(final Date date,
- final long timeSpan,
- @TimeConstants.Unit final int unit) {
- return millis2Date(date2Millis(date) + timeSpan2Millis(timeSpan, unit));
- }
- /**
- * Return the milliseconds differ time span by now.
- *
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the milliseconds differ time span by now
- */
- public static long getMillisByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
- return getMillis(getNowMills(), timeSpan, unit);
- }
- /**
- * Return the formatted time string differ time span by now.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span by now
- */
- public static String getStringByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
- return getStringByNow(timeSpan, getDefaultFormat(), unit);
- }
- /**
- * Return the formatted time string differ time span by now.
- *
- * @param timeSpan The time span.
- * @param format The format.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the formatted time string differ time span by now
- */
- public static String getStringByNow(final long timeSpan,
- @NonNull final DateFormat format,
- @TimeConstants.Unit final int unit) {
- return getString(getNowMills(), format, timeSpan, unit);
- }
- /**
- * Return the date differ time span by now.
- *
- * @param timeSpan The time span.
- * @param unit The unit of time span.
- * <ul>
- * <li>{@link TimeConstants#MSEC}</li>
- * <li>{@link TimeConstants#SEC }</li>
- * <li>{@link TimeConstants#MIN }</li>
- * <li>{@link TimeConstants#HOUR}</li>
- * <li>{@link TimeConstants#DAY }</li>
- * </ul>
- * @return the date differ time span by now
- */
- public static Date getDateByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
- return getDate(getNowMills(), timeSpan, unit);
- }
- /**
- * Return whether it is today.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isToday(final String time) {
- return isToday(string2Millis(time, getDefaultFormat()));
- }
- /**
- * Return whether it is today.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isToday(final String time, @NonNull final DateFormat format) {
- return isToday(string2Millis(time, format));
- }
- /**
- * Return whether it is today.
- *
- * @param date The date.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isToday(final Date date) {
- return isToday(date.getTime());
- }
- /**
- * Return whether it is today.
- *
- * @param millis The milliseconds.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isToday(final long millis) {
- long wee = getWeeOfToday();
- return millis >= wee && millis < wee + TimeConstants.DAY;
- }
- /**
- * Return whether it is leap year.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isLeapYear(final String time) {
- return isLeapYear(string2Date(time, getDefaultFormat()));
- }
- /**
- * Return whether it is leap year.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isLeapYear(final String time, @NonNull final DateFormat format) {
- return isLeapYear(string2Date(time, format));
- }
- /**
- * Return whether it is leap year.
- *
- * @param date The date.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isLeapYear(final Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int year = cal.get(Calendar.YEAR);
- return isLeapYear(year);
- }
- /**
- * Return whether it is leap year.
- *
- * @param millis The milliseconds.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isLeapYear(final long millis) {
- return isLeapYear(millis2Date(millis));
- }
- /**
- * Return whether it is leap year.
- *
- * @param year The year.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isLeapYear(final int year) {
- return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
- }
- /**
- * Return the day of week in Chinese.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the day of week in Chinese
- */
- public static String getChineseWeek(final String time) {
- return getChineseWeek(string2Date(time, getDefaultFormat()));
- }
- /**
- * Return the day of week in Chinese.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the day of week in Chinese
- */
- public static String getChineseWeek(final String time, @NonNull final DateFormat format) {
- return getChineseWeek(string2Date(time, format));
- }
- /**
- * Return the day of week in Chinese.
- *
- * @param date The date.
- * @return the day of week in Chinese
- */
- public static String getChineseWeek(final Date date) {
- return new SimpleDateFormat("E", Locale.CHINA).format(date);
- }
- /**
- * Return the day of week in Chinese.
- *
- * @param millis The milliseconds.
- * @return the day of week in Chinese
- */
- public static String getChineseWeek(final long millis) {
- return getChineseWeek(new Date(millis));
- }
- /**
- * Return the day of week in US.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the day of week in US
- */
- public static String getUSWeek(final String time) {
- return getUSWeek(string2Date(time, getDefaultFormat()));
- }
- /**
- * Return the day of week in US.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the day of week in US
- */
- public static String getUSWeek(final String time, @NonNull final DateFormat format) {
- return getUSWeek(string2Date(time, format));
- }
- /**
- * Return the day of week in US.
- *
- * @param date The date.
- * @return the day of week in US
- */
- public static String getUSWeek(final Date date) {
- return new SimpleDateFormat("EEEE", Locale.US).format(date);
- }
- /**
- * Return the day of week in US.
- *
- * @param millis The milliseconds.
- * @return the day of week in US
- */
- public static String getUSWeek(final long millis) {
- return getUSWeek(new Date(millis));
- }
- /**
- * Return whether it is am.
- *
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isAm() {
- Calendar cal = Calendar.getInstance();
- return cal.get(GregorianCalendar.AM_PM) == 0;
- }
- /**
- * Return whether it is am.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isAm(final String time) {
- return getValueByCalendarField(time, getDefaultFormat(), GregorianCalendar.AM_PM) == 0;
- }
- /**
- * Return whether it is am.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isAm(final String time,
- @NonNull final DateFormat format) {
- return getValueByCalendarField(time, format, GregorianCalendar.AM_PM) == 0;
- }
- /**
- * Return whether it is am.
- *
- * @param date The date.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isAm(final Date date) {
- return getValueByCalendarField(date, GregorianCalendar.AM_PM) == 0;
- }
- /**
- * Return whether it is am.
- *
- * @param millis The milliseconds.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isAm(final long millis) {
- return getValueByCalendarField(millis, GregorianCalendar.AM_PM) == 0;
- }
- /**
- * Return whether it is am.
- *
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isPm() {
- return !isAm();
- }
- /**
- * Return whether it is am.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isPm(final String time) {
- return !isAm(time);
- }
- /**
- * Return whether it is am.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isPm(final String time,
- @NonNull final DateFormat format) {
- return !isAm(time, format);
- }
- /**
- * Return whether it is am.
- *
- * @param date The date.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isPm(final Date date) {
- return !isAm(date);
- }
- /**
- * Return whether it is am.
- *
- * @param millis The milliseconds.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isPm(final long millis) {
- return !isAm(millis);
- }
- /**
- * Returns the value of the given calendar field.
- *
- * @param field The given calendar field.
- * <ul>
- * <li>{@link Calendar#ERA}</li>
- * <li>{@link Calendar#YEAR}</li>
- * <li>{@link Calendar#MONTH}</li>
- * <li>...</li>
- * <li>{@link Calendar#DST_OFFSET}</li>
- * </ul>
- * @return the value of the given calendar field
- */
- public static int getValueByCalendarField(final int field) {
- Calendar cal = Calendar.getInstance();
- return cal.get(field);
- }
- /**
- * Returns the value of the given calendar field.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @param field The given calendar field.
- * <ul>
- * <li>{@link Calendar#ERA}</li>
- * <li>{@link Calendar#YEAR}</li>
- * <li>{@link Calendar#MONTH}</li>
- * <li>...</li>
- * <li>{@link Calendar#DST_OFFSET}</li>
- * </ul>
- * @return the value of the given calendar field
- */
- public static int getValueByCalendarField(final String time, final int field) {
- return getValueByCalendarField(string2Date(time, getDefaultFormat()), field);
- }
- /**
- * Returns the value of the given calendar field.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @param field The given calendar field.
- * <ul>
- * <li>{@link Calendar#ERA}</li>
- * <li>{@link Calendar#YEAR}</li>
- * <li>{@link Calendar#MONTH}</li>
- * <li>...</li>
- * <li>{@link Calendar#DST_OFFSET}</li>
- * </ul>
- * @return the value of the given calendar field
- */
- public static int getValueByCalendarField(final String time,
- @NonNull final DateFormat format,
- final int field) {
- return getValueByCalendarField(string2Date(time, format), field);
- }
- /**
- * Returns the value of the given calendar field.
- *
- * @param date The date.
- * @param field The given calendar field.
- * <ul>
- * <li>{@link Calendar#ERA}</li>
- * <li>{@link Calendar#YEAR}</li>
- * <li>{@link Calendar#MONTH}</li>
- * <li>...</li>
- * <li>{@link Calendar#DST_OFFSET}</li>
- * </ul>
- * @return the value of the given calendar field
- */
- public static int getValueByCalendarField(final Date date, final int field) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(field);
- }
- /**
- * Returns the value of the given calendar field.
- *
- * @param millis The milliseconds.
- * @param field The given calendar field.
- * <ul>
- * <li>{@link Calendar#ERA}</li>
- * <li>{@link Calendar#YEAR}</li>
- * <li>{@link Calendar#MONTH}</li>
- * <li>...</li>
- * <li>{@link Calendar#DST_OFFSET}</li>
- * </ul>
- * @return the value of the given calendar field
- */
- public static int getValueByCalendarField(final long millis, final int field) {
- Calendar cal = Calendar.getInstance();
- cal.setTimeInMillis(millis);
- return cal.get(field);
- }
- private static final String[] CHINESE_ZODIAC =
- {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
- /**
- * Return the Chinese zodiac.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the Chinese zodiac
- */
- public static String getChineseZodiac(final String time) {
- return getChineseZodiac(string2Date(time, getDefaultFormat()));
- }
- /**
- * Return the Chinese zodiac.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the Chinese zodiac
- */
- public static String getChineseZodiac(final String time, @NonNull final DateFormat format) {
- return getChineseZodiac(string2Date(time, format));
- }
- /**
- * Return the Chinese zodiac.
- *
- * @param date The date.
- * @return the Chinese zodiac
- */
- public static String getChineseZodiac(final Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return CHINESE_ZODIAC[cal.get(Calendar.YEAR) % 12];
- }
- /**
- * Return the Chinese zodiac.
- *
- * @param millis The milliseconds.
- * @return the Chinese zodiac
- */
- public static String getChineseZodiac(final long millis) {
- return getChineseZodiac(millis2Date(millis));
- }
- /**
- * Return the Chinese zodiac.
- *
- * @param year The year.
- * @return the Chinese zodiac
- */
- public static String getChineseZodiac(final int year) {
- return CHINESE_ZODIAC[year % 12];
- }
- private static final int[] ZODIAC_FLAGS = {20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
- private static final String[] ZODIAC = {
- "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
- "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
- };
- /**
- * Return the zodiac.
- * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
- *
- * @param time The formatted time string.
- * @return the zodiac
- */
- public static String getZodiac(final String time) {
- return getZodiac(string2Date(time, getDefaultFormat()));
- }
- /**
- * Return the zodiac.
- *
- * @param time The formatted time string.
- * @param format The format.
- * @return the zodiac
- */
- public static String getZodiac(final String time, @NonNull final DateFormat format) {
- return getZodiac(string2Date(time, format));
- }
- /**
- * Return the zodiac.
- *
- * @param date The date.
- * @return the zodiac
- */
- public static String getZodiac(final Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int month = cal.get(Calendar.MONTH) + 1;
- int day = cal.get(Calendar.DAY_OF_MONTH);
- return getZodiac(month, day);
- }
- /**
- * Return the zodiac.
- *
- * @param millis The milliseconds.
- * @return the zodiac
- */
- public static String getZodiac(final long millis) {
- return getZodiac(millis2Date(millis));
- }
- /**
- * Return the zodiac.
- *
- * @param month The month.
- * @param day The day.
- * @return the zodiac
- */
- public static String getZodiac(final int month, final int day) {
- return ZODIAC[day >= ZODIAC_FLAGS[month - 1]
- ? month - 1
- : (month + 10) % 12];
- }
- private static long timeSpan2Millis(final long timeSpan, @TimeConstants.Unit final int unit) {
- return timeSpan * unit;
- }
- private static long millis2TimeSpan(final long millis, @TimeConstants.Unit final int unit) {
- return millis / unit;
- }
- static String millis2FitTimeSpan(long millis, int precision) {
- if (precision <= 0) return null;
- precision = Math.min(precision, 5);
- String[] units = {"天", "小时", "分钟", "秒", "毫秒"};
- if (millis == 0) return 0 + units[precision - 1];
- StringBuilder sb = new StringBuilder();
- if (millis < 0) {
- sb.append("-");
- millis = -millis;
- }
- int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
- for (int i = 0; i < precision; i++) {
- if (millis >= unitLen[i]) {
- long mode = millis / unitLen[i];
- millis -= mode * unitLen[i];
- sb.append(mode).append(units[i]);
- }
- }
- return sb.toString();
- }
- public static Date stringToDate(String strTime)
- throws ParseException {
- SimpleDateFormat formatter = getSafeDateFormat("yyyy-MM-dd");
- Date date = null;
- date = formatter.parse(strTime);
- return date;
- }
- public static long[] getHmsFromSecond(long timeSecond) {
- long[] result = new long[3];
- try{
- result[0] = timeSecond / 3600;
- timeSecond = timeSecond % 3600;
- long minute = timeSecond / 60;//得到分
- result[1] = minute;
- result[2] = timeSecond % 60;
- }catch (Exception e){
- e.printStackTrace();
- }
- return result;
- }
- public static String secondToTime(int second) {
- // int hour = second / 3600; // 得到分钟数
- // second = second % 3600;//剩余的秒数
- int minute = second / 60;//得到分
- second = second % 60;//剩余的秒
- return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
- }
- public static String msToTime(int ms) {
- return secondToTime(ms / 1000);
- }
- public static String msToTime(long ms) {
- return secondToTime(ms / 1000);
- }
- public static String secondToTime(long second) {
- // int hour = second / 3600; // 得到分钟数
- // second = second % 3600;//剩余的秒数
- long minute = second / 60;//得到分
- second = second % 60;//剩余的秒
- return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
- }
- public static boolean isLessThanTargetTime(String targetMinutes, String startTime) {
- try {
- long time = TimeUtils.string2Date(startTime).getTime();
- long cTime = System.currentTimeMillis();
- if (cTime >= time) {
- return true;
- }
- long limitMinutes = Long.parseLong(targetMinutes);
- if (Math.abs(time - cTime) < limitMinutes * 60 * 1000) {
- return true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- }
|