JAvaにおける時間の操作
- /*
- *java
- *gc.add(1,-1) .
- *gc.add(2,-1) .
- *gc.add(3.-1) .
- *gc.add(5,-1) .
- * . . .
- *GregorianCalendar add(int field,int amount) .
- *field , . .
- *amount .
- *
- * UseDate.java :
- */
-
-
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Locale;
- import java.text.ParseException;
- import java.text.ParsePosition;
- import java.text.SimpleDateFormat;
- public class UserDate {
-
- /*** ****/
-
- Date d=new Date();
- GregorianCalendar gc =new GregorianCalendar();
- SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd"); //
-
- //
- public String getLocalDate()
- {
- return sf.format(d);
- }
-
- //
- public String getYears()
- {
- gc.setTime(d);
- gc.add(1,+1);
- gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
-
- return sf.format(gc.getTime());
- }
-
- //
- public String getHalfYear()
- {
- gc.setTime(d);
- gc.add(2,+6);
- gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
-
- return sf.format(gc.getTime());
- }
-
- //
- public String getQuarters()
- {
- gc.setTime(d);
- gc.add(2,+3);
- gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
-
- return sf.format(gc.getTime());
- }
-
- //
- public String getDate(){
- gc.setTime(d);
- gc.add(5, +11);
- gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
-
- return sf.format(gc.getTime());
- }
-
- //
- public String getSimpleDateFormat(){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- String time = sdf.format(new Date());
-
- return time;
- }
-
- //
- public int day(){//s1 s2
- String s1 = "2011-09-10";
- String s2 = "2011-09-19";
-
- int day=0;
- try{
- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
- ParsePosition pos = new ParsePosition(0);
- ParsePosition pos1 = new ParsePosition(0);
- Date dt1=formatter.parse(s2,pos);
- Date dt2=formatter.parse(s1,pos1);
- long l = dt1.getTime() - dt2.getTime();
- day=(int)(l/(3600*24*1000));
- }catch(Exception e){
- System.out.println("exception"+e.toString());
- }
- return day;
- }
-
-
- public String StringToDateTime(){
- String myString = "Tue Oct 18 04:11:56 CST 2005 ";
- String s = null;
- try {
- SimpleDateFormat sdf = new SimpleDateFormat( "EEE MMM dd HH:mm:ss zzz yyyy ", Locale.US);
- Date d = sdf.parse(myString);
- Calendar c = Calendar.getInstance();
- c.setTime(d);
- s = new SimpleDateFormat( "yyyy-MM-dd ").format(c.getTime());
- System.out.println(s);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- return s;
- }
-
- //String Date
- public String StringToDateTime2(){
- String time = "2011-09-19";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
- Date timetoString = null;
-
- try {
- timetoString = sdf.parse(time);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- System.out.println(" Date :"+timetoString);
- String s = sdf.format(timetoString);
-
- return s;
- }
-
-
- public static void main(String[] args)
- {
- UserDate ud= new UserDate();
-
- System.out.println(" :"+ud.getSimpleDateFormat());
-
- System.out.println(" :"+ud.getLocalDate());
- System.out.println(" :"+ud.getYears());
- System.out.println(" :"+ud.getHalfYear());
- System.out.println(" :"+ud.getQuarters());
- System.out.println(" :"+ud.getDate());
- System.out.println(" :"+ud.day());
- System.out.println("String Date :"+ud.StringToDateTime());
- System.out.println(" :"+ud.StringToDateTime2());
- }
- }