Java初级笔记No.7之Java程序实例(时间处理与方法)

2018-08-08  本文已影响17人  wenmingxing

I、时间处理

1.1 格式化时间

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

package example;

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

public class SimpleDateFormatEmp {
    public static void main(String[] args) {
        Date date = new Date();
        String strDateFormat="yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
    
}
1.2 获取当前时间

使用Date类和SimpleDateFormat类的format(date)方法来输出当前时间:

package example;

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

public class NowTimeEmp {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");  //a为显式am,pm标记
        Date date = new Date();
        System.out.println("现在时间: " + sdf.format(date));
    }
}

1.3 获取年份、月份等

使用Calender类来输出年份、月份:

package example;

import java.util.Calendar;

public class CalendarEmp {
    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);
        System.out.println("一个月中第几天: " + dom);
        System.out.println("一年中第几天: " + doy);
        
    }
}

1.4 时间戳转换时间

使用SimpleDateFormat类的format()方法将时间戳转化成时间:

package example;

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

public class TimeStampEmp{
    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);
   }
}

II、方法

2.1 方法重载

这里与C++中的重载是一个定义,直接看实例:

package example;

public class MethodEmp {
    public static void main(String[] args) {
        MyClass t = new MyClass(3);
        t.info();
        t.info("重载方法");
        
        new MyClass();
    }
}

class MyClass {
    int height;
    MyClass() {
        System.out.println("无参数构造函数");
        height = 4;
    }
    
    MyClass(int i) {
        System.out.println("有参数构造函数: 房子的高度为 "  + i + "米");
        height = i;
    }
    
    void info() {
        System.out.println("房子的高度为: " + height + "米");
    }
    
    void info(String s) {
        System.out.println(s + " :房子的高度为" + height + "米");
    }
}
2.2 输出数组元素

通过重载,使得pringArray方法输出不同类型的数组:

package example;

public class printArrayEmp {
    public static void printArray(Integer[] inputArray) {
        for (Integer element : inputArray) {
            System.out.printf("%s" , element);
        }
        System.out.println();
    }
    
    public static void printArray(Double[] inputArray) {
        for (Double element : inputArray) {
            System.out.printf("%s ", element);          
        }
        System.out.println();
    }
    
    public static void printArray(Character[] inputArray) {
        for (Character element : inputArray) {
            System.out.printf("%s ", element);
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        Integer[] integerArray = {1,2,3,4,5,6};
        Double[] doubleArray = {1.1,2.2,3.3,4.4,5.5,6.6};
        Character[] characterArray = {'H','E','L','L','O'};
        
        printArray(integerArray);
        printArray(doubleArray);
        printArray(characterArray);
    }
}

2.3 方法重写

这里与C++中的重写一样,直接看实例:

package example;

public class OverrideEmp {
    public static void main(String[] args) {
        Figure f = new Figure(10, 10);
        Rectangle r = new Rectangle(9, 5);
        Figure figref;
        figref = f;
        System.out.println("Area is : " + figref.area());
        
        figref = r;
        System.out.println("Area is : " + figref.area());
    }
}

class Figure {
    double dim1;
    double dim2;
    Figure(double a, double b) {
        dim1 = a;
        dim2 = b;
    }
    Double area() {
        System.out.println("Inside area for figure.");
        return dim1*dim2;
    }
}

class Rectangle extends Figure {
    Rectangle(double a, double b) {
        super(a, b);    //调用super class的构造函数
    }
    
    Double area() {
        System.out.println("Inside area for rectangle.");
        return dim1*dim2;
    }
}

2.4 instanceof 关键字用法

instanceof是java的一个二元操作符,类似于==>等。
instanceof是java的保留关键字。它的作用是测试它左边的对象是否是右边类的实例,返回boolean的数据类型。

package example;
import java.util.ArrayList;
import java.util.Vector;

public class InstanceofEmp {
    public static void main(String[] args) {
        Object testObject = new ArrayList();
        displayObjectClass(testObject);
    }
    
    public static void displayObjectClass(Object o) {
        if (o instanceof Vector) {
            System.out.println("对象是Vector的实例");
        } 
        else if (o instanceof ArrayList) {
            System.out.println("对象是ArrayList的实例");
        }
        else {
            System.out.println("对象是" + o.getClass() + "的实例");
        }
    }
}

2.5 标签

Java中的标签是为循环设计的,是为了在多重循环中方便的使用break和continue。
可以使用标签调到指定位置:

package example;

public class LabelEmp {
    public static void main(String[] args) {
        String strSearch = "This is the string in which you have to search for a substring.";
        String substring = "substring";
        
        boolean found = false;
        int max = strSearch.length() - substring.length();
        
        testlb1:
            for (int i = 0; i <= max; ++i) {
                int length = substring.length();
                int j = i;
                int k = 0;
                while (length-- != 0) {
                    if (strSearch.charAt(j++) != substring.charAt(k++)) {
                        continue testlb1;   //标签的作用
                    }
                }
                found = true;
                break testlb1;  //与break作用一致
            }
        
        if (found) 
            System.out.println("Found!");
        else System.out.println("Not Found!");
    }
}

2.6 enum与switch

java创建枚举类型要使用enum关键字,隐含了所创建的类型都是java.lang.Enum类的子类。

package example;

enum Car {
    tata, audi, fiat
}

public class EnumSwitchEmp {
    public static void main(String[] args) {
        Car c = Car.tata;
        
        switch(c) {
        case tata:
            System.out.println("is tata");
            break;
        case audi:
            System.out.println("is audi");
            break;
        case fiat:
            System.out.println("is fiat");
            break;
        default:
            System.out.println("I dont know");
            break;
        }
        
    }
}

2.7 Varargs可变参数使用

定义实参个数可变的方法:只要在一个形参的类型与参数名之间加上三个连续的.就可以了:

package example;

public class VarargsEmp {
    static int sumvarargs(int... intArrays) {
        int sum= 0;
        for (int i = 0; i < intArrays.length; ++i) {
            sum += intArrays[i];
        }
        return sum;
    }
    public static void main(String[] args) {
        int sum = 0;
        sum = sumvarargs(new int[]{1,2,3,4,5});
        System.out.println(sum);
    }
}

上一篇下一篇

猜你喜欢

热点阅读