zoukankan      html  css  js  c++  java
  • 关于Java中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出:

    1 import java.util.Date;
    2 import java.text.SimpleDateFormat;
    3 
    4 public class NowString {
    5     public static void main(String[] args) {
    6         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
    7         System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
    8     }
    9 }

     二.用Calendar来获取和把一个String类性的时间转换成Date类型

     1 package test1;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Calendar;
     6 import java.util.Date;
     7 
     8 public class TestDate {
     9     public static void main(String[] args) {
    10         Date now = new Date();
    11         SimpleDateFormat dateFormat = new SimpleDateFormat(
    12                 "yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
    13 
    14         String hehe = dateFormat.format(now);
    15         System.out.println(hehe);
    16 
    17         Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改
    18 
    19         int year = c.get(Calendar.YEAR);
    20         int month = c.get(Calendar.MONTH);
    21         int date = c.get(Calendar.DATE);
    22         int hour = c.get(Calendar.HOUR_OF_DAY);
    23         int minute = c.get(Calendar.MINUTE);
    24         int second = c.get(Calendar.SECOND);
    25         System.out.println(year + "/" + month + "/" + date + " " + hour + ":"
    26                 + minute + ":" + second);
    27     }
    28 
    29     // 有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。
    30     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    31     Date time = null;
    32     //这是段代码块
    33     {
    34         try {
    35             time = sdf.parse(sdf.format(new Date()));
    36         } catch (ParseException e) {
    37             e.printStackTrace();
    38         }
    39     }
    40 }
  • 相关阅读:
    【Lintcode】112.Remove Duplicates from Sorted List
    【Lintcode】087.Remove Node in Binary Search Tree
    【Lintcode】011.Search Range in Binary Search Tree
    【Lintcode】095.Validate Binary Search Tree
    【Lintcode】069.Binary Tree Level Order Traversal
    【Lintcode】088.Lowest Common Ancestor
    【Lintcode】094.Binary Tree Maximum Path Sum
    【算法总结】二叉树
    库(静态库和动态库)
    从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4093622.html
Copyright © 2011-2022 走看看