ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:34:48 2017 +0800
Revision:
45:2aa9f933c8d2
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 45:2aa9f933c8d2 1 /*
TMBOY 45:2aa9f933c8d2 2 * Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
TMBOY 45:2aa9f933c8d2 3 *
TMBOY 45:2aa9f933c8d2 4 * Licensed under the Apache License, Version 2.0 (the "License");
TMBOY 45:2aa9f933c8d2 5 * you may not use this file except in compliance with the License.
TMBOY 45:2aa9f933c8d2 6 * You may obtain a copy of the License at
TMBOY 45:2aa9f933c8d2 7 *
TMBOY 45:2aa9f933c8d2 8 * http://www.apache.org/licenses/LICENSE-2.0
TMBOY 45:2aa9f933c8d2 9 *
TMBOY 45:2aa9f933c8d2 10 * Unless required by applicable law or agreed to in writing, software
TMBOY 45:2aa9f933c8d2 11 * distributed under the License is distributed on an "AS IS" BASIS,
TMBOY 45:2aa9f933c8d2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
TMBOY 45:2aa9f933c8d2 13 * See the License for the specific language governing permissions and
TMBOY 45:2aa9f933c8d2 14 * limitations under the License.
TMBOY 45:2aa9f933c8d2 15 */
TMBOY 45:2aa9f933c8d2 16 package com.baidu.duer.dcs.util;
TMBOY 45:2aa9f933c8d2 17
TMBOY 45:2aa9f933c8d2 18 import java.text.DateFormat;
TMBOY 45:2aa9f933c8d2 19 import java.text.ParseException;
TMBOY 45:2aa9f933c8d2 20 import java.text.SimpleDateFormat;
TMBOY 45:2aa9f933c8d2 21 import java.util.Date;
TMBOY 45:2aa9f933c8d2 22 import java.util.Locale;
TMBOY 45:2aa9f933c8d2 23
TMBOY 45:2aa9f933c8d2 24 /**
TMBOY 45:2aa9f933c8d2 25 * iso8601string 时间格式转换
TMBOY 45:2aa9f933c8d2 26 * <p>
TMBOY 45:2aa9f933c8d2 27 * Created by zhoujianliang01@baidu.com on 2017/6/5.
TMBOY 45:2aa9f933c8d2 28 */
TMBOY 45:2aa9f933c8d2 29 public class DateFormatterUtil {
TMBOY 45:2aa9f933c8d2 30 private static final String TAG = "ISO8601DateFormatter";
TMBOY 45:2aa9f933c8d2 31 private static final DateFormat DATE_FORMAT_1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.CHINESE);
TMBOY 45:2aa9f933c8d2 32 private static final DateFormat DATE_FORMAT_2 = new SimpleDateFormat("yyyy-MM-dd'T'HHmmssZ", Locale.CHINESE);
TMBOY 45:2aa9f933c8d2 33 private static final String UTC_PLUS = "+";
TMBOY 45:2aa9f933c8d2 34 private static final String UTC_MINUS = "-";
TMBOY 45:2aa9f933c8d2 35
TMBOY 45:2aa9f933c8d2 36 /**
TMBOY 45:2aa9f933c8d2 37 * 将iso8601string格式字符串时间转为Date
TMBOY 45:2aa9f933c8d2 38 *
TMBOY 45:2aa9f933c8d2 39 * @param iso8601string iso8601格式的时间字符串
TMBOY 45:2aa9f933c8d2 40 * @return Date Date类型表示的结果
TMBOY 45:2aa9f933c8d2 41 * @throws ParseException ParseException
TMBOY 45:2aa9f933c8d2 42 */
TMBOY 45:2aa9f933c8d2 43 public static Date toDate(String iso8601string) throws ParseException {
TMBOY 45:2aa9f933c8d2 44 iso8601string = iso8601string.trim();
TMBOY 45:2aa9f933c8d2 45 if (iso8601string.toUpperCase().indexOf("Z") > 0) {
TMBOY 45:2aa9f933c8d2 46 iso8601string = iso8601string.toUpperCase().replace("Z", "+0000");
TMBOY 45:2aa9f933c8d2 47 } else if (((iso8601string.indexOf(UTC_PLUS)) > 0)) {
TMBOY 45:2aa9f933c8d2 48 iso8601string = replaceColon(iso8601string, iso8601string.indexOf(UTC_PLUS));
TMBOY 45:2aa9f933c8d2 49 iso8601string = appendZeros(iso8601string, iso8601string.indexOf(UTC_PLUS), UTC_PLUS);
TMBOY 45:2aa9f933c8d2 50 } else if (((iso8601string.indexOf(UTC_MINUS)) > 0)) {
TMBOY 45:2aa9f933c8d2 51 iso8601string = replaceColon(iso8601string, iso8601string.indexOf(UTC_MINUS));
TMBOY 45:2aa9f933c8d2 52 iso8601string = appendZeros(iso8601string, iso8601string.indexOf(UTC_MINUS), UTC_MINUS);
TMBOY 45:2aa9f933c8d2 53 }
TMBOY 45:2aa9f933c8d2 54 LogUtil.d(TAG, "iso8601string:" + iso8601string);
TMBOY 45:2aa9f933c8d2 55 Date date;
TMBOY 45:2aa9f933c8d2 56 if (iso8601string.contains(":")) {
TMBOY 45:2aa9f933c8d2 57 date = DATE_FORMAT_1.parse(iso8601string);
TMBOY 45:2aa9f933c8d2 58 } else {
TMBOY 45:2aa9f933c8d2 59 date = DATE_FORMAT_2.parse(iso8601string);
TMBOY 45:2aa9f933c8d2 60 }
TMBOY 45:2aa9f933c8d2 61 return date;
TMBOY 45:2aa9f933c8d2 62 }
TMBOY 45:2aa9f933c8d2 63
TMBOY 45:2aa9f933c8d2 64 public static String toISO8601String(Date date) {
TMBOY 45:2aa9f933c8d2 65 return DATE_FORMAT_1.format(date);
TMBOY 45:2aa9f933c8d2 66 }
TMBOY 45:2aa9f933c8d2 67
TMBOY 45:2aa9f933c8d2 68 private static String replaceColon(String sourceStr, int offsetIndex) {
TMBOY 45:2aa9f933c8d2 69 if (sourceStr.substring(offsetIndex).contains(":")) {
TMBOY 45:2aa9f933c8d2 70 return sourceStr.substring(0, offsetIndex) + sourceStr.substring(offsetIndex).replace(":", "");
TMBOY 45:2aa9f933c8d2 71 }
TMBOY 45:2aa9f933c8d2 72 return sourceStr;
TMBOY 45:2aa9f933c8d2 73 }
TMBOY 45:2aa9f933c8d2 74
TMBOY 45:2aa9f933c8d2 75 private static String appendZeros(String sourceStr, int offsetIndex, String offsetChar) {
TMBOY 45:2aa9f933c8d2 76 if ((sourceStr.length() - 1) - sourceStr.indexOf(offsetChar, offsetIndex) <= 2) {
TMBOY 45:2aa9f933c8d2 77 return sourceStr + "00";
TMBOY 45:2aa9f933c8d2 78 }
TMBOY 45:2aa9f933c8d2 79 return sourceStr;
TMBOY 45:2aa9f933c8d2 80 }
TMBOY 45:2aa9f933c8d2 81 }