根據時間值 顯示對應的字串,顯示對應字串

來源:互聯網
上載者:User

根據時間值 顯示對應的字串,顯示對應字串

<pre name="code" class="java">/** * 擷取顯示時間 *  * @param time * @return */public String getDate(long time) {if (time <= 0) {return "";}// 日期處理Calendar calendar = Calendar.getInstance();calendar.setTime(new Date());int day = calendar.get(Calendar.DAY_OF_MONTH);int year = calendar.get(Calendar.YEAR);int hour = calendar.get(Calendar.HOUR_OF_DAY);int miunte = calendar.get(Calendar.MINUTE);int millisecond = calendar.get(Calendar.SECOND);int month = calendar.get(Calendar.MONTH)+ 1;// 評論日期Calendar commentCale = Calendar.getInstance();commentCale.setTime(new Date(time));int commDay = commentCale.get(Calendar.DAY_OF_MONTH);int commYear = commentCale.get(Calendar.YEAR);int commHour = commentCale.get(Calendar.HOUR_OF_DAY);int commMiunte = commentCale.get(Calendar.MINUTE);int commMonth = commentCale.get(Calendar.MONTH)+ 1;int commMillisecond = commentCale.get(Calendar.SECOND);// 剛剛// X秒前// X分鐘前// X小時前// 昨天// X月X日// 2014年//以往年份if (year - commYear >= 1) {// 去年 顯示年return commYear + "年";}// 今年時間if (year - commYear == 0) {// X月X日// 同月if (month != commMonth) {return commMonth + "月" + commDay + "日";}// return commMonth + "月" + commDay + "日";// 以往天數if (day - commDay == 1) {// 昨天return "昨天";}if (day - commDay == 0) {// 今天if (hour == commHour) {// 同一小時if (miunte == commMiunte) {// 同一分鐘if (millisecond == commMillisecond) {return "剛剛";}return millisecond - commMillisecond + "秒前";}return miunte - commMiunte + "分鐘前";}return hour - commHour + "小時前";}}// 一般不會到這步DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");return format.format(new Date(time));}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.