java日期時間封裝方法(java封裝流程)

本文目錄一覽:

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
UWPD的頭像UWPD
上一篇 2024-10-04 00:23
下一篇 2024-10-04 00:23

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 解決.net 6.0運行閃退的方法

    如果你正在使用.net 6.0開發應用程序,可能會遇到程序閃退的情況。這篇文章將從多個方面為你解決這個問題。 一、代碼問題 代碼問題是導致.net 6.0程序閃退的主要原因之一。首…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python中init方法的作用及使用方法

    Python中的init方法是一個類的構造函數,在創建對象時被調用。在本篇文章中,我們將從多個方面詳細討論init方法的作用,使用方法以及注意點。 一、定義init方法 在Pyth…

    編程 2025-04-29
  • Python創建分配內存的方法

    在python中,我們常常需要創建並分配內存來存儲數據。不同的類型和數據結構可能需要不同的方法來分配內存。本文將從多個方面介紹Python創建分配內存的方法,包括列表、元組、字典、…

    編程 2025-04-29
  • 用不同的方法求素數

    素數是指只能被1和自身整除的正整數,如2、3、5、7、11、13等。素數在密碼學、計算機科學、數學、物理等領域都有着廣泛的應用。本文將介紹幾種常見的求素數的方法,包括暴力枚舉法、埃…

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • 使用Vue實現前端AES加密並輸出為十六進制的方法

    在前端開發中,數據傳輸的安全性問題十分重要,其中一種保護數據安全的方式是加密。本文將會介紹如何使用Vue框架實現前端AES加密並將加密結果輸出為十六進制。 一、AES加密介紹 AE…

    編程 2025-04-29
  • Python學習筆記:去除字符串最後一個字符的方法

    本文將從多個方面詳細闡述如何通過Python去除字符串最後一個字符,包括使用切片、pop()、刪除、替換等方法來實現。 一、字符串切片 在Python中,可以通過字符串切片的方式來…

    編程 2025-04-29
  • 用法介紹Python集合update方法

    Python集合(set)update()方法是Python的一種集合操作方法,用於將多個集合合併為一個集合。本篇文章將從以下幾個方面進行詳細闡述: 一、參數的含義和用法 Pyth…

    編程 2025-04-29

發表回復

登錄後才能評論