在schema中datetime 的类型(符合 ISO 8601 格式)如何转换,如何通过其格式验证!

在schema中有定义 datetime 的类型 ,经常会出现 'yyyy-mm-dd hh:mm:ss' is not a valid value for 'dateTime'的错误 ,头疼 啊!怎么回事呢 ? 原来 在schema中的datetime 是符合 ISO 8601 格式的,它是一个国际标准。另外,使用此格式指定的日期时间值很明确。格式为:yyyy-mm-ddThh:mm:ss[.mmm]

/ **
 * 日期格式化工具.
*/
public class DatetimeFormatUtil {
 /***************************************************************************
  * The number of milliseconds in a minute.
  */
 private final static long MS_IN_MINUTE = 60 * 1000;

 /***************************************************************************
  * The number of milliseconds in an hour.
  */
 private final static long MS_IN_HOUR = 60 * 60 * 1000;

 /***************************************************************************
  * The number of milliseconds in a day.
  */
 // private final static long MS_IN_DAY = 24 * 60 * 60 * 1000;
 /**
  * 本地日期格式符号表
  */
 public final static DateFormatSymbols localDateFormatSymbols = new DateFormatSymbols(
   Locale.getDefault());

 /**
  * 月年格式
  */
 public static final SimpleDateFormat monthYearFormat = new SimpleDateFormat(
   "MMM yyyy", Locale.getDefault());

 /**
  * 年月格式
  */
 public static final SimpleDateFormat monthFormat = new SimpleDateFormat(
   "yyyy-MM");

 /**
  * 日格式
  */
 public static final SimpleDateFormat dayFormat = new SimpleDateFormat("d");

 /**
  * 时间格式
  */
 public static final SimpleDateFormat minuteFormat = new SimpleDateFormat(
   "HH:mm");

 /**
  * 时间格式
  */
 public static final SimpleDateFormat timeFormat = new SimpleDateFormat(
   "HH:mm:ss");

 /**
  * 24小时的时间 格式
  */
 public static final SimpleDateFormat time24Format = new SimpleDateFormat(
   "HH:mm:ss");

 /**
  * 星期格式
  */
 public static final SimpleDateFormat weekFormat = new SimpleDateFormat(
   "EEE");

 /**
  * 年月日 短格式
  */
 public static final SimpleDateFormat shortDateFormat = new SimpleDateFormat(
   "yy-MM-dd");

 /**
  * 年月日 长格式
  */
 public static final SimpleDateFormat longDateFormat = new SimpleDateFormat(
   "yyyy-MM-dd");

 /**
  * 年月日 时分秒 短格式
  */
 public final static SimpleDateFormat shortDatetimeFormat = new SimpleDateFormat(
   "yy-MM-dd HH:mm:ss");

 /**
  * 年月日 时分秒 长格式
  */
 public final static SimpleDateFormat longDatetimeFormat = new SimpleDateFormat(
   "yyyy-MM-dd HH:mm:ss");

 private final static SimpleDateFormat isoFormat_ = new SimpleDateFormat(
   "yyyy-MM-dd'T'HH:mm:ss");

 /***************************************************************************
  * Format the given date and return the resulting string in ISO 8601 format.
  * The format is as follows: "yyyy-MM-dd'T'HH:mm:ss.SSS[Z|[+|-]HH:mm]".
  *
  * @param inputDate
  *            The date to be converted into string format.
  * @return The formatted date/time string.
  */
 public static String isoFormat(Date inputDate) {

  // Setup the date format and convert the given date.
  // SimpleDateFormat dateFormat = new
  // SimpleDateFormat(ISO_FORMAT);
  String dateString = isoFormat_.format(inputDate);

  // Determine the time zone and concatenate the time zone
  // designator
  // onto the formatted date/time string.
  TimeZone tz = isoFormat_.getTimeZone();
  String tzName = tz.getDisplayName();
  if (tzName.equals("Greenwich Mean Time")) {
   dateString = dateString.concat("Z");
  } else {
   // Determine the hour offset. Add an hour if daylight
   // savings
   // is in effect.
   long tzOffsetMS = tz.getRawOffset();
   long tzOffsetHH = tzOffsetMS / MS_IN_HOUR;
   if (tz.inDaylightTime(inputDate)) {
    tzOffsetHH = tzOffsetHH + 1;
   }
   String hourString = String.valueOf(Math.abs(tzOffsetHH));
   if (hourString.length() == 1) {
    hourString = "0" + hourString;
   }

   // Determine the minute offset.
   long tzOffsetMMMS = tzOffsetMS % MS_IN_HOUR;
   long tzOffsetMM = 0;
   if (tzOffsetMMMS != 0) {
    tzOffsetMM = tzOffsetMMMS / MS_IN_MINUTE;
   }
   String minuteString = String.valueOf(tzOffsetMM);
   if (minuteString.length() == 1) {
    minuteString = "0" + minuteString;
   }

   // Determine the sign of the offset.
   String sign = "+";
   if (String.valueOf(tzOffsetMS).indexOf("+") != -1) {
    sign = "-";
   }

   dateString = dateString.concat(sign + hourString + ":"
     + minuteString);
  }

  return (dateString);
 }

 /***************************************************************************
  * Parse the given date/time string in ISO 8601 format and return the
  * resulting <code>Date</code> object. The format is as follows:
  * "yyyy-MM-dd'T'HH:mm:ss.SSS[Z|[+|-]HH:mm]".
  *
  * @param inputString
  *            The string to be parsed.
  * @return The resulting Date object.
  * @throws ParseException
  *             If the string does not match the date/time format.
  */
 public static Date isoParse(String inputString) throws ParseException {
  // Setup the date format.
  // SimpleDateFormat dateFormat = new
  // SimpleDateFormat(ISO_FORMAT);
  isoFormat_.setLenient(false);

  // The length of the input string should be at least 24
  // characters.
  if (inputString.length() < 24) {
   throw new ParseException(
     "An exception occurred because the input date/time string was not at least 24 characters in length.",
     inputString.length());
  }

  // Evaluate the the specified offset and set the time zone.
  String offsetString = inputString.substring(23);
  if (offsetString.equals("Z")) {
   isoFormat_.setTimeZone(TimeZone.getTimeZone("Greenwich Mean Time"));
  } else if (offsetString.startsWith("-") || offsetString.startsWith("+")) {
   SimpleDateFormat offsetFormat = new SimpleDateFormat();
   if (offsetString.length() == 3) {
    offsetFormat.applyPattern("HH");
   } else if (offsetString.length() == 6) {
    offsetFormat.applyPattern("HH:mm");
   } else {
    throw new ParseException(
      "An exception occurred because the offset portion was not the valid length of 3 or 6 characters.",
      25);
   }

   // Validate the given offset.
   offsetFormat.setLenient(false);
   // Date offsetDate = offsetFormat.parse(offsetString.substring(1));

   // Set the time zone with the validated offset.
   isoFormat_.setTimeZone(TimeZone.getTimeZone("GMT" + offsetString));
  } else {
   throw new ParseException(
     "An exception occurred because the offset portion of the input date/time string was not 'Z' or did not start with '+' or '-'.",
     24);
  }

  // Parse the given string.
  Date parseDate = isoFormat_.parse(inputString);

  return (parseDate);

//test

public static void main(String[] argv) {
  System.out.println("ISO format: "+ DatetimeFormatUtil.isoFormat(new Date()));
  DateFormat df = SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG,
    Locale.CANADA);
  System.out.println(df.format(new Date()));
 }
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值