JDK中的Date类的小误区和获取自定义日期的号数和毫秒转日期

来源:互联网 发布:autocad制图软件下载 编辑:程序博客网 时间:2024/04/28 21:54

用法类似:Date date=new Date(String string);//日期字符串,eg  “2015.02.03”

                    Date dat=new Date(Long timeMills);//毫秒数,相对格林尼治时间1970.1。1 00:00:00(GMT)

其中小误区:

1,date.getYear();//此方法返回的是相对于1900年(并非是完整的年或者是格林时间)。所以要或得最初的时间还得+1900.

2,date.getDay()// 此方法返回的是星期几而并非号数   eg,2015-06-17 (周三),调用返回的是3

返回自定义Date类的号数,eg  Date date=new Date(46666666666);这个日期所对应的的号数(天数)

/**获取此毫秒对应的日期如:2015-06-27则返回27 * @param timeInMillis * @return */public static int getDayOfMonth(Long timeInMillis){    Date dat=new Date(timeInMillis);    Calendar calendar=Calendar.getInstance();    calendar.setTimeInMillis(timeInMillis);    return calendar.get(Calendar.DAY_OF_MONTH);}

3,long------->date(毫秒转日期)

               Long timemills = new JSONObject(jsonArr.getJSONObject(i).getString("createdOn")).getLong("time");
                Date dat = new Date(timemills);
                GregorianCalendar gc = new GregorianCalendar();//GregorianCalendarCalendar 的一个具体子类
                gc.setTime(dat);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss ");
                String time = format.format(gc.getTime());


0 0
原创粉丝点击