码迷,mamicode.com
首页 > 编程语言 > 详细

Java中时间

时间:2017-06-24 21:49:20      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:text   for   util   seda   ted   xxx   描述   imei   lis   

/**

*观看API文档学习:Date中很多方法失效;Calendar顶替。

*

**/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
    public static void main(String[] args) throws ParseException {
       /**
        * 计算 "2012/3/17" "2012-5-28"间隔天数
        * 1.毫秒值获取相减
        * 2.获取毫秒值,字符串--》日期对象-->毫秒值
        */
       getDays();
    }
    public static void getSeconds(){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = df .parse("2017-06-25");
        //Date date1=new Date();
        Date date1 = df.parse("2017-06-24");
        Calendar cal = Calendar.getInstance();
        Calendar cal1=Calendar.getInstance();
        cal.setTime(date);
        cal1.setTime(date1);
        long timestamp = cal.getTimeInMillis();
        long timestamp1=cal1.getTimeInMillis();
        System.out.println(timestamp1-timestamp);       
    }  
    public static void getDays() throws ParseException{     
        String str_date1="2012/3/17";
        String str_date2="2012-5-28";
        //日期格式字符串解析成日期对象
        //1.自定义yyyy/MM/dd风格对象
        DateFormat dateFormat1=new SimpleDateFormat("yyyy/MM/dd");
        //2.定义一个默认风格
        DateFormat dateFormat2=DateFormat.getDateInstance(DateFormat.MEDIUM);
           //查错误验证  如果系统为英文默认是yy/mm/dd
            Date date=new Date();
            String str_date=dateFormat2.format(date);
            System.out.println(str_date);
        
        //3.对日期格式字符串进行解析
        Date date1=dateFormat1.parse(str_date1);
        Date date2=dateFormat2.parse(str_date2);
        //4.通过日期对象获取毫秒值
         long time1=date1.getTime();
         long time2=date2.getTime();
         //5.相减
         long time=Math.abs(time2-time1);
         //6.毫秒转day
         int day=(int) (time/1000/60/60/24);
        System.out.println(day);

    }
    public static void cal(){
        //演示日历
        Calendar ca=Calendar.getInstance();
    //    System.out.println(ca);
        //日历:将时间相关的东西以键值对的方式全部封装在一个map集合
        /*
         *
         * java.util.GregorianCalendar[time=1497856585181,areFieldsSet=true,
         * areAllFieldsSet=true,lenient=true,
         * zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
         * offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],
         * firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=5,WEEK_OF_YEAR=25,
         * WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=170,DAY_OF_WEEK=2,
         * DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=3,
         * HOUR_OF_DAY=15,MINUTE=16,SECOND=25,MILLISECOND=181,ZONE_OFFSET=28800000,DST_OFFSET=0]
         *
         */
        //对日历对象其中的日期进行定义
          //ca.set(Calendar.YEAR, 2015);
          //ca.set(2012, 2, 2);
        //日期偏移
        //ca.add(Calendar.YEAR, -4);
        //日期偏移比较狠
        //ca.add(Calendar.MONTH, 7);
        //showDate(ca);
        //任意一年2月有多少天?3月1日的前一天。
       //int year=2014;
       //ca.set(year, 2,1);
       //ca.add(Calendar.DAY_OF_MONTH, -1);
      // showDate(ca);
       //获取昨天的现在时刻。
        ca.add(Calendar.DAY_OF_MONTH, -1);
        showDate(ca);
    }
    public static void showDate(Calendar ca){ 
        int year=ca.get(Calendar.YEAR);
        int month=ca.get(Calendar.MONTH)+1;
        int day=ca.get(Calendar.DAY_OF_MONTH);
        int week=ca.get(Calendar.DAY_OF_WEEK);
        System.out.println(year+"年"+month+"月"+day+"日"+week);
        
    }
    public static void date() {
        Date date = new Date();
        System.out.println(date);
        // 格式化--日期转文本 解析--文本转日期
        // DateFormat 提供了很多类方法,以获得基于默认或给定语言环境和多种格式化风格的默认日期/时间 Formatter。
        // 格式化风格包括 FULL、LONG、MEDIUM 和 SHORT。方法描述中提供了使用这些风格的更多细节和示例。
        // 通过DateFormat类中的静态工厂方法获取实例

        DateFormat df = DateFormat.getDateInstance();

        // 如果格式一般我们还可以加入style
        df = DateFormat.getDateInstance(DateFormat.SHORT);
        // FULL:2017年6月19日 星期一
        // Long:2017年6月19日
        // MEDIUM:2017-6-19
        // SHORT:17-6-19
        // 转成自定义格式 XXXX/XX/XX 只能自定义对象 .使用子类//去SimpleDateFormat类总找日期和时间模式

        df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

        // 日期+时间
        DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.FULL,
                DateFormat.FULL);
        String dateformat1 = df1.format(date);
        System.out.println(dateformat1);

        // 使用DateFormat的format方法对日期对象进行格式化

        String dateformat = df.format(date);
        System.out.println(dateformat);
    }
    public static void date2() {
        // 毫秒值--》日期对象 构造函数 setTime方法
        long time = System.currentTimeMillis();
        System.out.println(time);
        Date date = new Date(time);
        System.out.println(date);
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
        String df_date = df.format(date);
        System.out.println(df_date);
        // 日期对象--》毫秒值 计算时间差 (时间戳)
        long time1 = date.getTime();
        System.out.println(time1);
    }
    public static void date1() throws ParseException {
        Date date = new Date();
        System.out.println(date);// 后台输出系统时间 但是格式看不懂
        // 格式转化 xxxx-yy-mm
        String str = "2016-09-07";
        DateFormat df = DateFormat.getDateInstance();

        Date date1 = df.parse(str);
        System.out.println(date1);
    }
}

Java中时间

标签:text   for   util   seda   ted   xxx   描述   imei   lis   

原文地址:http://www.cnblogs.com/elice/p/7074436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!