分享

JAVA常用的时间格式化方法

 跃来跃去 2008-11-17
/**
 * 根据想得到的时间格式返回string
 * @parma formatStyle 要格式化的格式 e.g:yyyy-MM-dd HH:mm:ss
 * @param date
 * @return
 */
public static String getFormatDate(String formatStyle) {
return new SimpleDateFormat(formatStyle).format(new Date().getTime());
}
/**
 * 根据想得到的时间格式返回string
 * @parma formatStyle 要格式化的格式 e.g:yyyy-MM-dd HH:mm:ss
 * @param time
 * @return
 */
public static String getFormatDate(String formatStyle,Date time) {
return new SimpleDateFormat(formatStyle).format(time);
}
/**
 * 把形式为yyyy-MM-dd HH:mm:ss的字符串转化为Date
 * @param strDate
 * @return
 */
public static Date convertStrToDate(String strDate) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate);
} catch (ParseException e) {
System.out.println("convert string to date error!");
e.printStackTrace();
}
return date;
}
/**
 * 得到日期字符串形式为20080908
 * @return
 */
public static  String getChinaDateString() {
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH)+1;
    int date = calendar.get(Calendar.DATE);
    String strYear = String.valueOf(year);
    String strMonth = month<10? "0"+ month : String.valueOf(month);       
    String strDate = date<10? "0"+date : String.valueOf(date);       
    return strYear+strMonth+strDate;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多