日期功能类DateFunction



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

//日期功能类
public class DateFunction {

    //获取当前日期
    public static Date getCurrentDate(){
        Date date=new Date();
        return date;
    }
    //返回当前年月日格式日期 yyyy-mm-dd
    public static String getCurrentShortDate() {
        DateFormat df1=null;
        Date date=new Date();
        df1=DateFormat.getDateInstance();//通过类提供的静态方法直接产生子类实例

        return df1.format(date);
    }
    //取得当前日期和时间 格式yyyy-mm-dd hh:mi:ss
    public static String getCurrentDateTime(){
        DateFormat df1=null;
        Date date=new Date();
        df1=DateFormat.getDateTimeInstance();
        return df1.format(date);
        }
    //返回"yyyy年xx月xx日"格式的日期
    public static String getCurrentChineseDate(){
       DateFormat df1=null;
       Date date=new Date();
       df1=DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh","CN"));//按照中国人习惯输出日期格式

        return df1.format(date);
    }
    //将字符串日期转换为日期类型,字符串格式 yyyy-mm-dd,如果转换失败,抛出异常
    public static Date convertDate(String currentDate){

        Date d=null;
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");

        try{
            d=sdf1.parse(currentDate);//把日期字符串中的日期部分抽取出来生成一个Date对象
        }catch(Exception e){
        }
        return d;

    }

    //将日期时间:yyyy-mm-dd hh:mi:ss转换为日期类型,如果转换失败,抛出转换异常
    public static Date convertToLongDate(String currentDate){
        Date d=null;
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try{
            d=sdf1.parse(currentDate);//把日期字符串中的日期部分抽取出来生成一个Date对象
        }catch(Exception e){
        }
        return d;

    }

  //将日期类型转换为字符串类型 格式yyy-mm-dd
    public static String convertToDateString(Date date){

        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd");
        String str=sdf1.format(date);//把日期按指定的模板格式格式化输出为字符串
        return str;
    }

    //将日期类型转换为字符串类型 格式yyy-mm-dd
    public static String convertToDateTimeString(Date date){

        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str=sdf1.format(date);//把日期按指定的模板格式格式化输出为字符串
        return str;
    }
}

public class DateFunctionTest {

    public static void main(String[] args){
        System.out.println(DateFunction.getCurrentDate());//Wed Jul 27 12:09:10 CST 2016
        System.out.println(DateFunction.getCurrentShortDate());//2016-7-27
        System.out.println(DateFunction.getCurrentDateTime());//2016-7-27 12:14:15
        System.out.println(DateFunction.getCurrentChineseDate());//2016年7月27日 星期三
        System.out.println(DateFunction.convertDate("2016-7-27"));//Wed Jul 27 00:00:00 CST 2016
        System.out.println(DateFunction.convertToLongDate("2010-10-19 10:11:30.345"));//Tue Oct 19 10:11:30 CST 2010
        System.out.println(DateFunction.convertToDateString(DateFunction.convertDate("2016-7-27")));//2016-00-27
        System.out.println(DateFunction.convertToDateTimeString(DateFunction.convertToLongDate("2010-10-19 10:11:30.345")));//2010-10-19 10:11:30
}
}



































  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值