zoukankan      html  css  js  c++  java
  • java-SimpleDateFormatDemo & BirthDemo

    java日期格式设置,以及案例BirthDemo

     1 package com.example;
     2 import java.text.ParseException;
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 /**
     6  * SimpleDateFormatDemo.java Description:根据给定的日期格式字符串,
     7  * 通过SimpleDateFormat在String和Date之间相互转换
     8  *
     9  * @author raizoo
    10  * Created on 17-7-17 下午9:24
    11  * @version 1.0
    12  * @since JDK8.0
    13  *
    14  * @thows Exception:无
    15  */
    16 public class SimpleDateFormatDemo {
    17     public static void main(String[] args) throws ParseException {
    18         Date date = new Date();
    19         System.out.println(date);  //输出Date默认的toString方法的返回值
    20 
    21         //希望输出格式:2014-8-13 21:13:47
    22         String dateformat = "yyyy-MM-dd HH:mm:ss E a";
    23 
    24         /**
    25          * 导入包:java.text.SimpleDateFormat
    26          * @param Stirng dateformat
    27          */
    28         SimpleDateFormat sdf = new SimpleDateFormat(dateformat);  //按dateformat格式转换--目标格式
    29         String str = sdf.format(date);  //format方法return值类型为string
    30         System.out.println(str);
    31 
    32         /**
    33          * parse(Stirng source) Description:
    34          * 通过simpledateform的parse方法,解析字符串str1的格式,并转化为date()方法的字符串日期输出格式.
    35          *
    36          * @param String source
    37          * @return Date类型 date
    38          * @thows Exception: java.text.ParseException
    39          */
    40         String str1 = "2009-08-19 20:19:22";
    41         String str2 = "yyyy-MM-dd HH:mm:ss";
    42         SimpleDateFormat simpform = new SimpleDateFormat(str2);
    43         Date dat = simpform.parse(str1);
    44         System.out.println(dat);
    45 
    46     }
    47 }

     案例:BirthDemo

     1 package com.example;
     2 import java.util.Scanner;
     3 import java.util.Date;
     4 import java.text.SimpleDateFormat;
     5 import java.text.ParseException;
     6 /**
     7  * BirthDemo.java Description:输入一个生日,计算距今为止多少天.
     8  *
     9  * @author raizoo
    10  * Created on 17-7-17 下午10:47
    11  * @version 1.0
    12  * @since JDK8.0
    13  *
    14  * @thows Exception:无
    15  */
    16 public class BirthDemo {
    17    public static void main(String[] args) throws ParseException {
    18        //输入生日
    19        Scanner scan = new Scanner(System.in);
    20        System.out.print("输入一个生日(格式:1980-08-03 12:31:17):");
    21        String str1 = scan.nextLine();  //写入1980-08-13 12:31:17
    22        System.out.println();
    23        String str2 = "yyyy-MM-dd HH:mm:ss";
    24 
    25        //把输入的生日格式转换成date()方法格式
    26        SimpleDateFormat simpform = new SimpleDateFormat(str2);
    27        Date date1 = simpform.parse(str1);
    28        long dat = date1.getTime();  //生日距计算机元年毫秒数
    29 
    30        Date date = new Date();
    31        long da = date.getTime();  //目前距计算机元年毫秒数
    32 
    33        long target = (da-dat)/(24*60*60*1000);  //距今xx天
    34        int year = (int)target/365;
    35        System.out.println("距今"+year+"年");
    36    }
    37 }
  • 相关阅读:
    Windows性能计数器应用
    Azure Oracle Linux VNC 配置
    Azure 配置管理系列 Oracle Linux (PART6)
    Azure 配置管理系列 Oracle Linux (PART5)
    Azure 配置管理系列 Oracle Linux (PART4)
    Azure 配置管理系列 Oracle Linux (PART3)
    Azure 配置管理系列 Oracle Linux (PART2)
    vagrant多节点配置
    docker基本操作
    LINUX开启允许对外访问的网络端口命令
  • 原文地址:https://www.cnblogs.com/DeRozan/p/7198323.html
Copyright © 2011-2022 走看看