logo

שיעור תאריכים בג'אווה (עם דוגמאות)

תאריך הכיתה מייצג רגע ספציפי בזמן עם דיוק של אלפיות השנייה. מחלקת התאריכים של חבילת java.util מיישמת ממשק שיבוט הניתן לסדרה והשוואה. הוא מספק בונים ושיטות להתמודד עם תאריך ושעה עם Java. קונסטרוקטורים
    תַאֲרִיך(): יוצר אובייקט תאריך המייצג תאריך ושעה נוכחיים. תאריך (אלפיות שניות ארוכות): יוצר אובייקט תאריך עבור אלפיות השנייה הנתונה מאז 1 בינואר 1970 00:00:00 GMT. תאריך (int שנה int חודש int תאריך) תאריך (int שנה int חודש int תאריך int hrs int min) תאריך (int שנה int חודש int תאריך int hrs int min int sec) תאריך (מחרוזת) הערה: The last 4 constructors of the Date class are Deprecated. Java
    // Java program to demonstrate constuctors of Date import java.util.*; public class Main {  public static void main(String[] args)  {  Date d1 = new Date();  System.out.println('Current date is ' + d1);  Date d2 = new Date(2323223232L);  System.out.println('Date represented is '+ d2 );  } } 
    Output:
    Current date is Tue Jul 12 18:35:37 IST 2016 Date represented is Wed Jan 28 02:50:23 IST 1970 
    שיטות חשובות
      בוליאני לאחר (תאריך תאריך):בדיקות אם התאריך הנוכחי הוא לאחר התאריך הנתון. בוליאני לפני (תאריך תאריך):בדיקות אם התאריך הנוכחי הוא לפני התאריך הנתון. int השוואה (תאריך תאריך):משווה את התאריך הנוכחי לתאריך נתון. מחזיר 0 אם תאריך הטיעון שווה לתאריך; ערך פחות מ- 0 אם התאריך הוא לפני טיעון התאריך; וערך גדול מ- 0 אם התאריך הוא לאחר טיעון התאריך. זמן רב (): מחזיר את מספר אלפיות השנייה מאז 1 בינואר 1970 00:00:00 GMT המיוצג על ידי אובייקט תאריך זה. סתיו בטל (זמן רב): משנה את התאריך והשעה הנוכחיים לזמן הנתון.
    Java
    // Program to demonstrate methods of Date class import java.util.*; public class Main {  public static void main(String[] args)  {  // Creating date  Date d1 = new Date(2000 11 21);  Date d2 = new Date(); // Current date  Date d3 = new Date(2010 1 3);  boolean a = d3.after(d1);  System.out.println('Date d3 comes after ' +  'date d2: ' + a);  boolean b = d3.before(d2);  System.out.println('Date d3 comes before '+  'date d2: ' + b);  int c = d1.compareTo(d2);  System.out.println(c);  System.out.println('Miliseconds from Jan 1 '+  '1970 to date d1 is ' + d1.getTime());  System.out.println('Before setting '+d2);  d2.setTime(204587433443L);  System.out.println('After setting '+d2);  } } 
    Output:
    Date d3 comes after date d2: true Date d3 comes before date d2: false 1 Miliseconds from Jan 1 1970 to date d1 is 60935500800000 Before setting Tue Jul 12 13:13:16 UTC 2016 After setting Fri Jun 25 21:50:33 UTC 1976