当前位置:   article > 正文

javaBasics篇(三)_java basics

java basics

The first java program.

1.输出当前时间:


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HelloData {

        public static void main(String[] args) throws ParseException {
            Calendar now = Calendar.getInstance();
            System.out.println("年: " + now.get(Calendar.YEAR));
            System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
            System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
            System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
            System.out.println("分: " + now.get(Calendar.MINUTE));
            System.out.println("秒: " + now.get(Calendar.SECOND));
            System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
            System.out.println(now.getTime());

            Date d = new Date();
            System.out.println(d);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateNowStr = sdf.format(d);
            System.out.println("格式化后的日期:" + dateNowStr);

            String str = "2012-1-13 17:26:33";  //要跟上面sdf定义的格式一样
            Date today = sdf.parse(str);
            System.out.println("字符串转成日期:" + today);
        }
    }


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在这里插入图片描述

2.输出当前时间:

import java.util.Date;

public class Hello {
    public static void main(String[] args) {
        System.out.println("hello data is"+":"+"data"+new Date());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/631291
推荐阅读
相关标签
  

闽ICP备14008679号