All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.dreampie.common.util.TimeUtils Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy