DateUtil--日期转换工具

来源:互联网 发布:现在做网络销售怎么样 编辑:程序博客网 时间:2024/06/05 14:12

DateUtil–日期转换工具

DateUtil.java

public class DateUtil {    private static final long S = 1000L;//一秒    private static final long MIN = 60 * S;//一分钟    private static final long H = 60 * MIN;//一小时    private static final long D = 24 * H;//一天    private static final long Y = 365 * D;//365天    //@by zhiqiang: 2016/5/27 定义日期的格式    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    private static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");    private static SimpleDateFormat monthFormat = new SimpleDateFormat("MM-dd HH:mm");    /**     * @by zhiqiang 2016/5/27 转换date数据格式    可以写在工具类中     */    public static String changeDateFormat(String date, String type) {        //将传过来的字符串转化一次        Date old = null;        try {            //解析字符串            sdf.setTimeZone(TimeZone.getTimeZone(type));            old = sdf.parse(date);        } catch (ParseException e) {            e.printStackTrace();        }        //@by zhiqiang: 2016/5/27 获取当前日期        Date newD = new Date();        //@by zhiqiang: 2016/5/27 如果当前时间在传递过来的时间之前        if (newD.before(old)) {            return yearFormat.format(old);        }        return parseDate(old, newD);    }    /**     * @by zhiqiang 2016/6/13  根据当前时间的毫秒值计算     */    private static String parseDate(Date old, Date newD) {        if (old != null && newD != null) {            final long oldTime = old.getTime();            final long newTime = newD.getTime();            final long time = newTime - oldTime;            if (time <= MIN) {    //1分钟之内 just                return "just";            } else if (time <= H) {    //1小时之内 几分钟之前                int minter = (int) (time / MIN);                return minter + " " + "分钟前";            } else if (time <= D) {//1天之内 几小时之前                int hour = (int) (time / H);                return hour + " 小时前";            } else if (time <= 7 * D) {//7天之内                int day = (int) (time / D);                if (day == 1) {//一天前                    return "昨天";                } else {                    return day + " 天前";                }            } else if (time <= Y) {//365天之内                sdf.setTimeZone(TimeZone.getDefault());                return monthFormat.format(old);            } else {                sdf.setTimeZone(TimeZone.getDefault());                return yearFormat.format(old);            }        }        return "";    }}

显示日期

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView tv_old = (TextView) findViewById(R.id.tv_old);        TextView tv_new = (TextView) findViewById(R.id.tv_new);        String oldDateString = "2016-06-20 16:09:08";        String change = DateUtil.changeDateFormat(oldDateString,"GMT:+08:00");        tv_old.setText(oldDateString);        tv_new.setText(change);    }}
0 0
原创粉丝点击