java的日期判定

判定二十四小时和十二小时格式:当是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 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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值