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);
String s = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
有兩個 String 類型的變量 都是日期 。然後需要調用getDaysBetween函數取得這兩個日期間的間隔天數,我要怎麼把那兩個String 類型的變量 轉換成函數可以接受的參數類型呢?
String Now_date=sdf.format(now); 兩個日期都用使用SimpleDateFormat規定好格式了
public void Format()
{
sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(dStr);
} catch (ParseException pe) {
System.out.println(pe.getMessage());
}
}
String Now_date=sdf.format(now);
Date d1 = sdf.parse(Ex_date);
Date d2 = sdf.parse(Now_date);
GregorianCalendar d11 = new GregorianCalendar();
GregorianCalendar d22 = new GregorianCalendar();
d11.setTime(d1);
d22.setTime(d2);
int day=getDaysBetween(d11,d22);
先把String 轉成Date Date再轉成GregorianCalendar 類型,才能調用那個 別人寫好的函數!