cn.dreampie.common.util.TimeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfinal-dreampie Show documentation
Show all versions of jfinal-dreampie Show documentation
jfinal shiro-freemarker plugins
package cn.dreampie.common.util;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
* Created by wangrenhui on 14-4-21.
*/
public class TimeUtils {
private static TimeUtils timeUtils = new TimeUtils();
private TimeUtils() {
}
public static TimeUtils me() {
return timeUtils;
}
/**
* 字符串转换为时间格式
*
* @param dateStr date str
* @return datetime
*/
public DateTime toDateTime(String dateStr) {
DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime date = df.parseDateTime(dateStr);
return date;
}
public DateTime toDateTime(String dateStr, String format) {
if (ValidateUtils.me().isNullOrEmpty(format)) {
format = "yyyy-MM-dd HH:mm:ss";
}
DateTimeFormatter df = DateTimeFormat.forPattern(format);
DateTime date = df.parseDateTime(dateStr);
return date;
}
/**
* 时间转换为字符串
*
* @param date date
* @return datestr
*/
public String toString(DateTime date) {
DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = date.toString(df);
return dateStr;
}
/**
* @param date date
* @param format format
* @return datestr
*/
public String toString(DateTime date, String format) {
if (ValidateUtils.me().isNullOrEmpty(format)) {
format = "yyyy-MM-dd HH:mm:ss";
}
DateTimeFormatter df = DateTimeFormat.forPattern(format);
String dateStr = date.toString(df);
return dateStr;
}
}