Java by API/java.util/Date — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:11, 31 мая 2010

Date: after(Date when)

/*
Mon Jan 01 00:00:00 PST 2001 is after Sat Jan 01 00:00:00 PST 2000
 * */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String[] a) throws Exception{
    DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
    Date d1 = df.parse("2001-01-01");
    Date d2 = df.parse("2000-01-01");
    String relation;
    if (d1.equals(d2))
      relation = "the same date as";
    else if (d1.before(d2))
      relation = "before";
    else if (d1.after(d2))
      relation = "after";
    System.out.println(d1 + " is " + relation + " " + d2);
  }
}





Date: before(Date when)

/*
Mon Jan 01 00:00:00 PST 2001 is after Sat Jan 01 00:00:00 PST 2000
 * */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String[] a) throws Exception{
    DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
    Date d1 = df.parse("2001-01-01");
    Date d2 = df.parse("2000-01-01");
    String relation;
    if (d1.equals(d2))
      relation = "the same date as";
    else if (d1.before(d2))
      relation = "before";
    else if (d1.after(d2))
      relation = "after";
    System.out.println(d1 + " is " + relation + " " + d2);
  }
}





Date: diff (Not a method)

/*
The 21st century (up to Thu May 11 14:14:02 PDT 2006) is 1956 days old.
 * */
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
  public static void main(String[] a) throws Exception {
    Date d1 = new GregorianCalendar(2000,11,31,23,59).getTime();
    Date today = new Date();
    long diff = today.getTime() - d1.getTime();
    System.out.println("The 21st century (up to " + today + 
      ") is " + (diff / (1000*60*60*24)) + " days old.");
  }
}





Date: equals(Object anotherDate)

/*
Mon Jan 01 00:00:00 PST 2001 is after Sat Jan 01 00:00:00 PST 2000
 * */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String[] a) throws Exception{
    DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
    Date d1 = df.parse("2001-01-01");
    Date d2 = df.parse("2000-01-01");
    String relation;
    if (d1.equals(d2))
      relation = "the same date as";
    else if (d1.before(d2))
      relation = "before";
    else if (d1.after(d2))
      relation = "after";
    System.out.println(d1 + " is " + relation + " " + d2);
  }
}





Date: getTime()

/*
 * Output: 
13263
 */
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date currentDate = new Date();
    long msec = currentDate.getTime();
    
    long days = msec/(24 * 60 * 60 * 1000);
    System.out.println(days);
  }
}





Date: setTime(long miliseconds)

/*
 * Output:
Thu Aug 03 13:27:11 PDT 2006
 */
import java.util.*;
public class MainClass {
  public static void main(String args[]) {
    Date date = new Date();
    long msec = date.getTime();
    msec += 100 * 24 * 60 * 60 * 1000L;
    date.setTime(msec);
    System.out.println(date);
  }
}





new Date()

/*
 * Output: 
Tue Apr 25 13:25:27 PDT 2006
Wed Dec 31 16:00:00 PST 1969
 */
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date currentDate = new Date();
    System.out.println(currentDate);
    // Get date object initialized to the epoch (Jan 1 1970)
    Date epoch = new Date(0);
    System.out.println(epoch);
  }
}





new Date(int intValue)

/*
 * Output: 
Tue Apr 25 13:25:27 PDT 2006
Wed Dec 31 16:00:00 PST 1969
 */
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date currentDate = new Date();
    System.out.println(currentDate);
    // Get date object initialized to the epoch (Jan 1 1970)
    Date epoch = new Date(0);
    System.out.println(epoch);
  }
}