本文目錄一覽:
java簡單作業題
public class MyDate {
private int year ;
private int month ;
private int day ;
public MyDate(){}
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public String toString() {
return “MyDate ==”+year+”-“+month+”-“+day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
public class MyTime {
public static void main(String[] args) {
MyTime time = new MyTime(14,53,20);
System.out.println(time.toString());
}
private int hour;
private int minute;
private int second;
public MyTime() {
}
public MyTime(int hour, int minute, int second) {
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String toString() {
return “current time==”+hour + “:” + minute + “:” + second;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
public class FullTime {
public static void main(String[] args) {
MyDate myDate = new MyDate(2007, 10, 2);
MyTime myTime = new MyTime(14,17,35);
FullTime fullTime = new FullTime(myDate,myTime);
System.out.println(fullTime);
}
private MyDate myDate;
private MyTime myTime;
public FullTime(MyDate myDate, MyTime myTime) {
super();
this.myDate = myDate;
this.myTime = myTime;
}
public String toString() {
String text = myDate.getYear() + “年” + myDate.getMonth() + “月”
+ myDate.getDay() + “日” + myTime.getHour() + “時”
+ myTime.getMinute() + “分” + myTime.getSecond() + “秒”;
return text;
}
public MyDate getMyDate() {
return myDate;
}
public void setMyDate(MyDate myDate) {
this.myDate = myDate;
}
public MyTime getMyTime() {
return myTime;
}
public void setMyTime(MyTime myTime) {
this.myTime = myTime;
}
}
第4題,你自己想辦法吧。主要知識點:
1、繼承
2、super和final,這個只是表面的東西,說到底還是java中overrides(重寫)的要求
3、通過多層間接的繼承,你要知道的是 對象被實例化的順序。
求一個java的封裝時間的好一點的通用類代碼
import java.util.*;
import java.text.*;
public class Timer {
/**
* 獲得當前時間
* @return String
*/
public static String getDate()
{
Calendar date =Calendar.getInstance();
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String str = sim.format(date.getTime());
return str;
}
/**
* 字符串轉換為時間
* @param date String
* @return Date
*/
public static Date getDate(String date) {
try {
SimpleDateFormat localTime = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date1 = localTime.parse(date);
return date1;
}
catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 取得秒數
*/
public static Long getDateDiff_Second(Date d1, Date d2) {
return (d2.getTime() – d1.getTime()) / 1000;
}
/**
* 取得分鐘
* @param d1 Date
* @param d2 Date
* @return Long
*/
public static Long getDateDiff_Minute(Date d1, Date d2) {
return (d2.getTime() – d1.getTime()) / (1000 * 60);
}
/**
* 取得小時
* @param d1 Date
* @param d2 Date
* @return Long
*/
public static Long getDateDiff_Hour(Date d1, Date d2) {
return (d2.getTime() – d1.getTime()) / (1000 * 3600);
}
public static Long getDateDiff_Hour(String d1, String d2) {
return (getDate(d2).getTime() – getDate(d1).getTime()) / (1000 * 3600);
}
/**
*取得天數
* @param d1 Date
* @param d2 Date
* @return Long
*/
public static Long getDateDiff_Day(Date d1, Date d2) {
return (d2.getTime() – d1.getTime()) / (1000 * 3600*24);
}
public static Long getDateDiff_Day(String d1, String d2) {
return (getDate(d2).getTime() – getDate(d1).getTime()) / (1000 * 3600*24);
}
/**
*取得星期間隔
* @param d1 Date
* @param d2 Date
* @return Long
*/
public static Long getDateDiff_Week(Date d1, Date d2) {
return (d2.getTime() – d1.getTime()) / (1000 * 3600*24*7);
}
/**
* 取得當前時間的 間隔多少小時之後的時間
* @param hour int
* @return String
*/
public static String getDateAdd(int hour)
{
Calendar calendar=Calendar.getInstance();
java.text.SimpleDateFormat sd=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
calendar.set(Calendar.HOUR,hour+calendar.get(Calendar.HOUR));
String enddate =sd.format(calendar.getTime());
return enddate;
}
/**
* 取得當前時間的 間隔多少小時之後的時間
* @param hour int
* @return String
*/
public static String getDateAdd(String starttime,int hour)
{
Calendar calendar=Calendar.getInstance();
java.text.SimpleDateFormat sd=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
calendar.setTime(getDate(starttime));
calendar.set(Calendar.HOUR,hour+calendar.get(Calendar.HOUR));
String date =sd.format(calendar.getTime());
return date;
}
/**
* 獲得時間格式的文件名稱
* @return String
*/
public static String getFileName()
{
Calendar date =Calendar.getInstance();
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(“yyyyMMdd_hhmmss”);
String str = sim.format(date.getTime());
return str;
}
//獲得月日
public static String get_MM_DD(String s)
{
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date;
String str=””;
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat(“[MM-dd]”);
str=sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
finally
{
return str;
}
}
//獲得年月日
public static String get_YYYY_MM_DD(String s)
{
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date;
String str=””;
try {
date = sim.parse(s);
sim = new java.text.SimpleDateFormat(“[yyyy-MM-dd]”);
str=sim.format(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
finally
{
return str;
}
}
public static void main(String[] args)
{
}
}
java編程之怎樣把Long轉換成Date的日期格式
Long類型的時間轉換為date,可以通過SimpleDateFormat對象對格式進行定義,然後創建一個Date類型的對象封裝時間,再通過SimpleDateFormat對象的format(date)方法就可以獲取指定的日期格式了。
/**
* 把毫秒轉化成日期
* @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
* @param millSec(毫秒數)
* @return
*/
private String transferLongToDate(String dateFormat,Long millSec){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date= new Date(millSec);
return sdf.format(date);
}
可以試試我這個
Java中如何設置Date對象的年月日
Date
public Date(int year,
int month,
int day)
參數:
year – year 減去 1900,它必須是 0 到 8099 之間的數。(注意,8099 是由 9999 減去 1900 得到的。)
month – 0 到 11 之間的數
day – 1 到 31 之間的數
測試代碼如下:
import java.util.Date;
public class Test {
public static void main(String args[]){
Date date = new Date(2010-1900,1,10);
System.out.println(date);
}
}
運行結果:
Wed Feb 10 00:00:00 CST 2010
希望對你有幫助。。。。。。仍有問題可以HI我。。。。
原創文章,作者:UWPD,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/140320.html