Java数组与Date类,SimpleDateFormat类和Calendar类的使用

一维数组的创建需要经过声明数组和为数组分配元素两个步骤。

一维数组的声明

整型数组:int a[],int[] a;
双浮点数叔叔:double b[];
字符串数组:String s[];
对象数组:person[] p;
int a[5];//非法,java语言声明数组是不能指定其长度。

为数组分配元素

int[] a;
a = new int[4];
就像这样

public class TextArr {
 public static void main(String[] args) {
    int[] myArray;
    myArray = new int[3];
    Date[] date;
    date = new Date[2];
    date[0] = new Date(1,1,1);    
 }
 }
class Date{
 int year, month, day;
 Date(int y,  int m, int d) {
  year = y;
  month = m;
  day = d;
  }
  }
一维数组的初始化

int[] a={1,2,3,4};
String[ ] b = {" 欢迎“, ”java“, ”hello“};

二维数组的声明及初始化

int[ ] [ ] arr = new int[3][ ];
arr[0] = new int[4];
arr[1] = new int[5];
arr[2] = new int[6];

Date类与SimpleDateFormat类搭配使用

首先要说一下Date类有参的构造方法为Date(long time),time单位是毫秒,1秒 = 1000 毫秒。
也就是说Date date = new Date(0);这里的date表示的时间是1970年1月1日早上八点,如果其中的参数是负数,则会表示1970年1月1日早上八点之前的时间。(时区是CST,即中国标准时间)。

import java.util.Date;

public class DateTest01 {
    public static void main(String[] args) {
        Date date01 = new Date();
        System.out.println(date01);
        //无参的date01会直接输出当前时间:Wed May 13 01:00:53 CST 2020

        Date date02 = new Date(0);
        System.out.println(date02);
        //输出为:Thu Jan 01 08:00:00 CST 1970

        Date date03 = new Date(1000 * 60 * 60 * 24 * 365L);
        System.out.println(date03);
        //输出为:Fri Jan 01 08:00:00 CST 1971

        Date date04 = new Date(- 1000 * 60 * 60 * 24 * 365L);
        System.out.println(date04);
        //输出为:Wed Jan 01 08:00:00 CST 1969
    }
}

其中SimpleDateFormat可以改变Date类输出的格式:

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

public class DateTest {
    public static void main(String[] args) throws InterruptedException {
        Date data = new Date();
        System.out.println(data);
        //直接输出date:Wed May 13 01:03:25 CST 2020
        SimpleDateFormat sdf01 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format01 = sdf01.format(data);
        System.out.println(format01);
        //改变date格式后:2020-05-13 01:03:25
        SimpleDateFormat sdf02 = new SimpleDateFormat("yyyy-MM-dd a");
        String format02 = sdf02.format(data);
        System.out.println(format02);
        //改变date格式后:2020-05-13 上午
        SimpleDateFormat sdf03 = new SimpleDateFormat("E yyyy-MM-dd a");
        String format03 = sdf03.format(data);
        System.out.println(format03);
        //改变date格式后:星期三 2020-05-13 上午
    }
}

日期和时间格式由日期和时间模式字符串指定,以下是模式字符串的种类
在这里插入图片描述

通过Calendar类获取时间

首先Calendar类是一个抽象类,可以通过调用 getInstance() 静态方法获取一个 Calendar 对象。

import java.util.Calendar;
import java.util.Date;

public class CalenderTest01 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();//创建Calendar对象
        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH)+1;//获取月份,0表示1月
        int day = calendar.get(Calendar.DAY_OF_MONTH);//获取日期
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//获取小时
        int minute = calendar.get(Calendar.MINUTE);//获取分钟
        int second = calendar.get(Calendar.SECOND);//获取秒数
        System.out.println("当前时间:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
        //输出:当前时间:2020-5-13 13:15:26

        //  Calendar 类提供了 getTime() 方法,用来获取 Date 对象
        Calendar ca = Calendar.getInstance();
        Date time = ca.getTime();
        System.out.println(time);
        //输出:Wed May 13 13:15:26 CST 2020

        //  完成 Calendar 和 Date 的转换,还可通过 getTimeInMillis() 方法
        Calendar ca1 = Calendar.getInstance();
        long timeInMillis = calendar.getTimeInMillis();
        Date date = new Date(timeInMillis);
        System.out.println(date);
        //输出:Wed May 13 13:15:26 CST 2020
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值