zoukankan      html  css  js  c++  java
  • Java从入门到实战之(23)时间处理之练习

    1、格式化时间(SimpleDateFormat)

    如何使用 SimpleDateFormat 类的 format(date) 方法来格式化时间?

    package Java_Learn.File;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main1 {
        public static void main(String[] args) {
            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            String msg="当前时间是:";
            formatTime(msg,date,strDateFormat);
        }
        private static void formatTime(String msg,Date date,String strDateFormat)
        {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);
            System.out.println(msg+simpleDateFormat.format(date));
        }
    }

    结果:

    "C:Program FilesJavajdk1.8.0_221injava.exe" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2019.3.3libidea_rt.jar=52237:C:Program FilesJetBrainsIntelliJ IDEA 2019.3.3in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_221jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_221jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_221jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_221jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_221jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_221jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_221jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_221jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_221jrelibext
    ashorn.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_221jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_221jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_221jrelibjce.jar;C:Program FilesJavajdk1.8.0_221jrelibjfr.jar;C:Program FilesJavajdk1.8.0_221jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_221jrelibjsse.jar;C:Program FilesJavajdk1.8.0_221jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_221jrelibplugin.jar;C:Program FilesJavajdk1.8.0_221jrelib
    esources.jar;C:Program FilesJavajdk1.8.0_221jrelib
    t.jar;C:appWorkSpace	argetclasses;C:Program Files (x86)scalalibscala-actors-2.11.0.jar;C:Program Files (x86)scalalibscala-actors-migration_2.11-1.1.0.jar;C:Program Files (x86)scalalibscala-library.jar;C:Program Files (x86)scalalibscala-parser-combinators_2.11-1.0.1.jar;C:Program Files (x86)scalalibscala-reflect.jar;C:Program Files (x86)scalalibscala-swing_2.11-1.0.1.jar;C:Program Files (x86)scalalibscala-xml_2.11-1.0.1.jar;C:
    epositoryorgscala-langscala-library2.11.0scala-library-2.11.0.jar" Java_Learn.File.Main1
    当前时间是:2020-10-26 11:42:24
    
    Process finished with exit code 0

    2、当前时间

    如何获取当前时间?

    package Java_Learn.File;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main1 {
        public static void main(String[] args) {
            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            String msg="当前时间是:";
            formatTime(msg,date,strDateFormat);
            formatTime1(msg,date,strDateFormat);
        }
        private static void formatTime(String msg,Date date,String strDateFormat)
        {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);
            System.out.println(msg+simpleDateFormat.format(date));
        }
        private static void formatTime1(String msg,Date date,String strDateFormat)
        {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);
            simpleDateFormat.applyPattern(strDateFormat);// a为am/pm的标记
            System.out.println(msg+simpleDateFormat.format(date));
        }
    }

    结果:

    "C:Program FilesJavajdk1.8.0_221injava.exe" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2019.3.3libidea_rt.jar=52284:C:Program FilesJetBrainsIntelliJ IDEA 2019.3.3in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_221jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_221jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_221jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_221jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_221jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_221jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_221jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_221jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_221jrelibext
    ashorn.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_221jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_221jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_221jrelibjce.jar;C:Program FilesJavajdk1.8.0_221jrelibjfr.jar;C:Program FilesJavajdk1.8.0_221jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_221jrelibjsse.jar;C:Program FilesJavajdk1.8.0_221jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_221jrelibplugin.jar;C:Program FilesJavajdk1.8.0_221jrelib
    esources.jar;C:Program FilesJavajdk1.8.0_221jrelib
    t.jar;C:appWorkSpace	argetclasses;C:Program Files (x86)scalalibscala-actors-2.11.0.jar;C:Program Files (x86)scalalibscala-actors-migration_2.11-1.1.0.jar;C:Program Files (x86)scalalibscala-library.jar;C:Program Files (x86)scalalibscala-parser-combinators_2.11-1.0.1.jar;C:Program Files (x86)scalalibscala-reflect.jar;C:Program Files (x86)scalalibscala-swing_2.11-1.0.1.jar;C:Program Files (x86)scalalibscala-xml_2.11-1.0.1.jar;C:
    epositoryorgscala-langscala-library2.11.0scala-library-2.11.0.jar" Java_Learn.File.Main1
    当前时间是:2020-10-26 11:44:53
    当前时间是:2020-10-26 11:44:53
    
    Process finished with exit code 0

    3、年、月、日等时间的处理

    如何获取年、月、日等时间格式?

    import java.util.Calendar;
     
    public class Main {
        public static void main(String[] args) {
            Calendar cal = Calendar.getInstance();
            int day = cal.get(Calendar.DATE);
            int month = cal.get(Calendar.MONTH) + 1;
            int year = cal.get(Calendar.YEAR);
            int dow = cal.get(Calendar.DAY_OF_WEEK);
            int dom = cal.get(Calendar.DAY_OF_MONTH);
            int doy = cal.get(Calendar.DAY_OF_YEAR);
     
            System.out.println("当期时间: " + cal.getTime());
            System.out.println("日期: " + day);
            System.out.println("月份: " + month);
            System.out.println("年份: " + year);
            System.out.println("一周的第几天: " + dow);  // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
            System.out.println("一月中的第几天: " + dom);
            System.out.println("一年的第几天: " + doy);
        }
    }

    结果:

    当期时间: Fri Mar 27 21:44:15 CST 2015
    日期: 27
    月份: 3
    年份: 2015
    一周的第几天: 6
    一月中的第几天: 27
    一年的第几天: 86

    4、时间戳转换成时间

    如何使用 SimpleDateFormat 类的 format() 方法将时间戳转换成时间。

    日期和时间模式(注意大小写,代表的含义是不同的):

    • yyyy:年
    • MM:月
    • dd:日
    • hh:1~12小时制(1-12)
    • HH:24小时制(0-23)
    • mm:分
    • ss:秒
    • S:毫秒
    • E:星期几
    • D:一年中的第几天
    • F:一月中的第几个星期(会把这个月总共过的天数除以7)
    • w:一年中的第几个星期
    • W:一月中的第几星期(会根据实际情况来算)
    • a:上下午标识
    • k:和HH差不多,表示一天24小时制(1-24)
    • K:和hh差不多,表示一天12小时制(0-11)
    • z:表示时区
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    public class Main{
        public static void main(String[] args){
            Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));      // 时间戳转换成时间
            System.out.println("格式化结果:" + sd);
     
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
            String sd2 = sdf2.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            System.out.println("格式化结果:" + sd2);
       }
    }

    结果:

    格式化结果:2018-07-10 12:17:34
    格式化结果:2018 年 07 月 10 日 12 时 17 分 34 秒
    作者:大码王

    -------------------------------------------

    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•?ω•?)っ???!

  • 相关阅读:
    Java.io 包(字节流)
    Java 集合框架(常用数据结构)
    Java.util 包(Date 类、Calendar类、Random类)
    Java.lang 包 (包装类、String类、Math类、Class类、Object类)
    Java 多态(接口)
    maxcompute troubleshoot
    maxcompute
    文件命名
    weblogic修改ServerName
    设计模式---策略模式
  • 原文地址:https://www.cnblogs.com/huanghanyu/p/13877699.html
Copyright © 2011-2022 走看看