Summer Java study notes (1)

1. Java array: You must first declare the array variable before you can use the array in the program.

(1) Create an array statement - declare variables and assign values ​​​​together

//dataType为数据类型 arraySize定义数组长度
dataType[] arrayRefVar = new dataType[arraySize];
//直接定义数组内容
dataType[] arrayRefVar = {value0, value1, ..., valuek};

(2) Processing arrays - the element types of arrays and the size of arrays are determined, so when processing array elements, we usually use basic loops or For-Each loops

//一维数组使用for-each语句打印
double[] myList = {1.9, 2.9, 3.4, 3.5};
for (double element: myList) {
    System.out.println(element);
}

//二维数组使用for-each语句打印
//仅定义了含有多少个一维数组
String[][] s = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
//s[0]这个一维数组含 Good 和 Luck(说法不太准确,但可以这么理解)
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
for (String[] x:s)
    for (String a:x)
         System.out.print(a+" ");//Good Luck to you !

(3) Multi-dimensional array——A multi-dimensional array can be regarded as an array of arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array, as shown above.

2. Java date time

There are two ways of Java date and time, one is the Date class, the other is the Calendar class

(1) Date class

Import java.util.Date is required before using the Date class; but generally IDEs will automatically reference it after using the method.

① Get the current date and time

//获取当前日期
Date date = new Date();
//使用 toString() 函数显示日期时间 Sat May 27 20:51:21 CST 2023
System.out.println(date.toString());

② Use SimpleDateFormat to format the date

//SimpleDateFormat 允许你选择任何用户自定义日期时间格式来运行。
Date dNow = new Date( );
//完整的表示 年 月 日 时 分 秒 如果不是完整表示,会有不同的输出
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-dd-MM hh:mm:ss");
SimpleDateFormat ft1 = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间为: " + ft.format(dNow));
System.out.println("当前时间为: " + ft1.format(dNow));
//当前时间为: 2023-27-05 08:54:19
//当前时间为: 2023-05-27 08:54:19

③ Java dormancy - can achieve date update, but need to throw an exception statement

int i = 0;
try{
    while(i<5){
        SDF();
        //以毫秒为单位  这里是停顿3s后重新打印
        Thread.sleep(1000*3);
        i++;
     }}
catch (Exception ex){
    System.out.println("Got an exception!");
}

(2) Calendar class

The function of the Calendar class is much more powerful than the Date class, and the implementation method is also more complicated than the Date class

① Create a Calendar class for the current date of the system and a Calendar class for a specified date

        The month in the Calendar class starts from 0, that is, 0 is January

//默认是当前日期
Calendar c = Calendar.getInstance();

//创建一个代表2013年6月12日的Calendar对象
//Calendar类中 月份 从 0 开始
Calendar c1 = Calendar.getInstance();
c1.set(2013, 6 - 1, 12);

②Calendar class object field type

Calendar.YEAR years
Calendar.MONTH month
Calendar.DATE date
Calendar.DAY_OF_MONTH Date, which has exactly the same meaning as the above field
Calendar.HOUR 12 hour clock
Calendar.HOUR_OF_DAY 24 hour clock
Calendar.MINUTE minute
Calendar.SECOND Second
Calendar.DAY_OF_WEEK Day of the week

Use Add to set the replacement of the date

//设置当前日期
Calendar c = Calendar.getInstance();
c.set(2002,0,10);
//日期加上10天
c.add(Calendar.DATE,10);
//20
System.out.println(c.get(Calendar.DATE));

Guess you like

Origin blog.csdn.net/laodaye_xiaolizi/article/details/130905610