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

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

Версия 17:43, 31 мая 2010

new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz")

 
/*
 * Output:
11:27:37
28 Apr 2006 11:27:37 PDT
Fri Apr 28 2006
 */
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));
  }
}





new SimpleDateFormat("E MMM dd yyyy")

 
/*
 * Output:
11:27:37
28 Apr 2006 11:27:37 PDT
Fri Apr 28 2006
 */
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));
  }
}





new SimpleDateFormat("hh:mm:ss")

 
/*
 * Output:
11:27:37
28 Apr 2006 11:27:37 PDT
Fri Apr 28 2006
 */
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));
  }
}





new SimpleDateFormat(String pattern, Locale locale)

 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static Calendar parseTimestamp(String timestamp) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.US);
    Date d = sdf.parse(timestamp);
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal;
  }
  public static void main(String a[]) throws Exception {
    String timestampToParse = "24-Feb-2009 17:39:35";
    System.out.println("Timestamp : " + timestampToParse);
    SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("Calendar : " + sdf.format(parseTimestamp(timestampToParse).getTime()));
  }
}





new SimpleDateFormat("yyyy-MM-dd")

 
       
/*
 Output: 
 
 on Tue Oct 01 00:00:00 PDT 2013 in  Vancouver, B.C.
 on Sun Mar 01 00:00:00 PST 1248 in  Ottawa, ON
 on Mon Jun 06 00:00:00 PST 1323 in  Toronto, ON
*/       
       
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String[] a) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "2013-10-01 Vancouver, B.C.", 
                       "1248-03-01 Ottawa, ON",
                       "1323-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
      ParsePosition pp = new ParsePosition(0);
      Date d = formatter.parse(input[i], pp);
      if (d == null) {
        System.err.println("Invalid date in " + input[i]);
        continue;
      }
      String location = input[i].substring(pp.getIndex());
      System.out.println(" on " + d + " in " + location);
    }
  }
}





SimpleDateFormat: applyPattern(String pattern)

 
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();
    String era[] = { "BCE", "CE" };
    dfs.setEras(era);
    sdf.setDateFormatSymbols(dfs);
    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
  }
}





SimpleDateFormat: format(Date d)

 
/*
 * Output:
11:27:37
28 Apr 2006 11:27:37 PDT
Fri Apr 28 2006
 */
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));
  }
}





SimpleDateFormat: parse(String text, ParsePosition pos)

 
       
/*
 Output: 
 
 on Tue Oct 01 00:00:00 PDT 2013 in  Vancouver, B.C.
 on Sun Mar 01 00:00:00 PST 1248 in  Ottawa, ON
 on Mon Jun 06 00:00:00 PST 1323 in  Toronto, ON
*/       
       
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String[] a) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "2013-10-01 Vancouver, B.C.", 
                       "1248-03-01 Ottawa, ON",
                       "1323-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
      ParsePosition pp = new ParsePosition(0);
      Date d = formatter.parse(input[i], pp);
      if (d == null) {
        System.err.println("Invalid date in " + input[i]);
        continue;
      }
      String location = input[i].substring(pp.getIndex());
      System.out.println(" on " + d + " in " + location);
    }
  }
}





SimpleDateFormat: setDateFormatSymbols(DateFormatSymbols newFormatSymbols)

 
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();
    String era[] = { "BCE", "CE" };
    dfs.setEras(era);
    sdf.setDateFormatSymbols(dfs);
    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
  }
}