• 构造方法充当临时对象&Calendar的使用


    View Code
    package test1;

    import java.util.Calendar;

    public class Date {
    int year;
    int month;
    int day;
    int week;
    int hour;
    int minute;
    int second;

    // 跟类同名的构造方法
    public Date(int year, int month, int day, int week, int hour, int minute,
    int second) {
    this.year = year;
    this.month = month;
    this.day = day;
    this.week = week;
    this.hour = hour;
    this.minute = minute;
    this.second = second;
    }

    public static void main(String[] args) {
    Date date = date(2011, 12, 26);
    System.out.println(date.year + "年" + date.month + "月" + date.day + "日 "
    + date.hour + "点" + date.minute + "分" + date.second + "秒 周"
    + date.week);
    }

    public static Date date(int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    // 月份要注意减1
    cal.set(year, month - 1, day);
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    // 星期注意减一
    int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    Date date = new Date(year, month, day, week, hour, minute, second);
    return date;
    }
    }
  • 相关阅读:
    Spring jar包功能
    maven pom.xml详细介绍,必须留一份
    redis基本类型以及优点特性
    如何实现网站的防盗链?
    php几种常见排序算法
    批量去除文件的BOM头
    Keepalived实现双机热备
    nginx配置文件详解
    源码编译安装lnmp环境
    nginx实现https网站设置
  • 原文地址:https://www.cnblogs.com/xiaxinggege/p/2297889.html
Copyright © 2020-2023  润新知