判定二十四小时和十二小时格式:当是H时表示二十四小时制,h表示十二小时制
写道
public class Date24 {
public static void main(String[]args)
{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String c = sdf.format(date);
System.out.println(c);
}
}
public static void main(String[]args)
{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String c = sdf.format(date);
System.out.println(c);
}
}
public class Date12 {
public static void main(String[]args)
{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//小时地方是小写字母h;
String c = sdf.format(date);
System.out.println(c);
}
}
判定输入的String字符串是否是合法日期:
package date;
import java.text.SimpleDateFormat;
/**
* Created by taoyongpan on 2016/10/12.
*/
public class JudgeDate {
public static void main(String[]args)
{
String str="2007-5-12";
if(isDate(str,"yyyy.MM.dd")|isDate(str,"yyyy-MM-dd"))
System.out.print("This is true");
else
System.out.println(str);
}
public static boolean isDate(String str_input,String rDateFormat){
if (!isNull(str_input)) {
SimpleDateFormat format = new SimpleDateFormat(rDateFormat);
format.setLenient(false);
try {
format.format(format.parse(str_input));
} catch (Exception e) {
return false;
}
return true;
}
return false;
}
public static boolean isNull(String str){
if(str==null)
return true;
else
return false;
}
}