Java Date-time processing

Source: Internet
Author: User
Java Date Time processing one, time-related classes
    • Java.lang.System
    • Java.util.Date
    • Java.util.Calendar
    • Java.util.GregorianCalendar
    • Java.util.TimeZone
    • Java.text.DateFormat
    • Java.text.DateSimpleFormat
    • Java.sql.Date
    • Java.sql.Time
    • Java.sql.Timestamp
Ii. the relationship between the various classes Iii. common problems and solutions 1, what is Utc/ut?
    • UTC: World Standard Time
    • UT: World Time
2. What is GMT?
    • Greenwich Standard Time
3, how to get the current time?
   //在1970年1月1日UTC之间的当前时间和午夜之间的差异,以毫秒为单位。   long ctm = System.currentTimeMillis();   //分配一个 Date对象并对其进行初始化,以便它表示分配的时间,测量到最近的毫秒。   Date d = new Date();   //使用默认时区和区域设置获取日历。 Calendar返回的是基于默认时区的当前时间,默认为FORMAT区域设置。   Calendar cal = Calendar.getInstance();  //Calendar是抽象类,不能直接实例化      System.out.println(ctm);  //1535808760490   System.out.println(d);  //Sat Sep 01 21:32:40 CST 2018   System.out.println(cal);  //包含所有日期相关的属性,可以通过get()方法获得对应属性的值
4, how to format the time?
    Date d = new Date();     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");  // yyyy-MM-dd hh-mm-ss可以替换,具体类型可以查找Java API    String tssdf = sdf.format(d);
5. How do I convert a string to a date/time format?
    • The method of getting a single time date in Java.util.Date has been deprecated, and to get such values, you can use the related methods in Java.util.Calendar.

    • Example:

   Calendar cj=new GregorianCalendar(TimeZone.getTimeZone("PRC"));  //中国时间   int hod=cj.get(Calendar.HOUR_OF_DAY);  //该方法返回int型   System.out.println("24小时制小时: "+hod);  //24小时制小时   System.out.println("12小时制小时: "+cj.get(Calendar.HOUR));  //12小时制小时   System.out.print(cj.get(Calendar.HOUR_OF_DAY)+" 时 ");  //12小时制小时   System.out.print(cj.get(Calendar.MINUTE)+" 分 ");  //分钟   System.out.println(cj.get(Calendar.SECOND)+" 秒");  //秒   System.out.println("毫秒: "+cj.get(Calendar.MILLISECOND));  //毫秒   System.out.print(cj.get(Calendar.YEAR)+" 年 ");  //年   System.out.print((cj.get(Calendar.MONTH)+1)+" 月 ");  //月   System.out.println(cj.get(Calendar.DATE)+" 日");  //日
    • Example output:
    24小时制小时: 22    12小时制小时: 10    22 时 56 分 7 秒    毫秒: 559    2018 年 9 月 1 日
6. How do I handle the date/time obtained from the database?
    • Date formatting:

      Date d = res.getDate("stu_date");  //获取日期(只包含年月日)SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");String dsdf = sdf.format(d);  // 2018年07月25日
    • Time formatting:

      Time t = res.getTime("stu_date");  //获取时间(只包含时分秒)SimpleDateFormat sdf = new SimpleDateFormat("hh时mm分ss秒");String tsdf = sdf.format(t);  // 12时13分35秒
    • Full time formatting:

      Timestamp ts = res.getTimestamp("stu_date");SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");String tssdf = sdf.format(ts);

Java Date-time processing

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.