Java High imitation Weibo date display format, Date helper class

Source: Internet
Author: User

Original: Java High imitation Weibo date display format, Date helper class

Source code: Http://www.zuidaima.com/share/1550463377902592.htm

Imitation Sina Weibo date formatting tool class:

For example:

1. Just

2. XX minutes ago

3. HH hours ago

4. Today hh:mm

5. Yesterday hh:mm

6. The day before hh:mm

7. Last month YYYY-MM-DD hh:mm

8. Last Yyyy-mm-dd hh:mm

9. Yyyy-mm-dd hh:mm

Today's code, the structure is still very rough, just realized the function. The next refactoring supports the format descriptor before it is sent up.

Package Com.zuidaima.haohui.common.utils;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.date;import java.util.locale;/** * * Import Java.text.SimpleDateFormat; * @author Www.zuidaima.com**/import Java.util.calendar;public class super2{public static void Main (String args[]) {Calen  Dar cal = Calendar.getinstance ();  n is the number of weeks postponed, 1 this week, 1 is postponed forward one week, 2 next week, and so on int n = 2;  String Monday;  Cal.add (Calendar.date, n*7); Want to weeks, here will pass a few calendar.monday (Tuesday ...  ) Cal.set (Calendar.day_of_week,calendar.monday);  Monday = new SimpleDateFormat ("Yyyy-mm-dd"). Format (Cal.gettime ()); System.out.println (Monday); }} * @project Baidamei * @author Cevencheng <[email protected]> * @create 2012-11-10 a.m. 1:12:21 */public class D ateutil {public static void main (string[] args) throws ParseException {//simpledateformat Dfs = new SimpleDateFormat ("yyyy -mm-dd HH:mm:ss ");//java.util.date begin = Dfs.parse (" 2004-01-02 11:30:24 ");//java.util.date end = Dfs. Parse ("2004-03-26 13:31:40");//long between = (End.gettime ()-begin.gettime ())/1000;//divided by 1000 for conversion into seconds////long day = bet Ween/(3600);//long hour = between% (per * 3600)/3600;//long minute = between% 3600/60;//long second = between % 60/60;//system.out.println ("+ Day +" days "+ Hour +" hours "+ Minute +" min "+ Second +" SEC ");//system.out.println (Begin.gett IME ());////system.out.println (Getdaysbeforenow (New Date ())); SimpleDateFormat df = new SimpleDateFormat ("YYYY-MM-DD");D ate Date = Df.parse ("2012-11-18"); Calendar cal = Calendar.getinstance (); Cal.add (Calendar.date, -7); Cal.set (Calendar.day_of_week, calendar.monday); Monday System.out.println (Df.format (Cal.gettime ())) Cal.set (Calendar.day_of_week, calendar.sunday); Weekend System.out.println (Df.format (Cal.gettime ()));} /** * Interval calculation * */public static String getdaysbeforenow (date date) {Long sysTime = Long.parselong (new SimpleDateFormat ("yy Yymmddhhmmss "). Format (new Date ())), Long ymdhms = Long.parselong (New SimpleDateFormat (" YyyymmddhhmmsS "). Format (date); String stryear = "year ago"; String strmonth = "month ago"; String strday = "days ago"; String strhour = "Hour Ago"; String strminute = "minutes ago"; try {if (ymdhms = = 0) {return "";} Long between = (systime/10000000000l)-(ymdhms/10000000000l); if (between > 0) {return between + stryear;} between = (systime/100000000l)-(ymdhms/100000000l); if (between > 0) {return between + strmonth;} between = (systime/1000000l)-(ymdhms/1000000l); if (between > 0) {return between + strday;} between = (systime/10000)-(ymdhms/10000); if (between > 0) {return between + strhour;} between = (systime/100)-(ymdhms/100); if (between > 0) {return between + Strminute;} Return "1" + Strminute;}     catch (Exception e) {return "";}}}


Java High imitation Weibo date display format, Date helper class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.