1. 程式人生 > >格式化時間樣式的字串

格式化時間樣式的字串

好記性不如爛筆頭(不喜勿噴),有好的可以給我,我們一起進步

package com.o2o.util;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Create with IntelliJ IDEA.
 *
 * @author: 
[email protected]
* Date: 2019/1/2 * Time: 13:44 */ public class DateUtils { public final static LocalDateTime stringParseLocalDateTime(String dateTime) { if (dateTime == null || "".equals(dateTime)) return null; return parseLocalDateTime(dateTime); } private static LocalDateTime parseLocalDateTime(String dateTime) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String deleteString = ""; LocalDateTime parse = null; if (dateTime.length() == 14) { parse = LocalDateTime.parse(dateTime, dtf); return parse; } if (dateTime.length() == 8) { dateTime = dateTime + "000000"; parse = LocalDateTime.parse(dateTime, dtf); return parse; } if (dateTime.length() == 10 && dateTime.charAt(4) == '-' && dateTime.charAt(7) == '-') { char delChar = '-'; for (int i = 0; i < dateTime.length(); i++) { if (dateTime.charAt(i) != delChar) { deleteString += dateTime.charAt(i); } } deleteString = deleteString + "000000"; parse = LocalDateTime.parse(deleteString, dtf); return parse; } if (dateTime.length() == 10 && dateTime.charAt(4) == '/' && dateTime.charAt(7) == '/') { char delChar = '/'; for (int i = 0; i < dateTime.length(); i++) { if (dateTime.charAt(i) != delChar) { deleteString += dateTime.charAt(i); } } deleteString = deleteString + "000000"; parse = LocalDateTime.parse(deleteString, dtf); return parse; } if (dateTime.length() == 19 && dateTime.charAt(4) == '-' && dateTime.charAt(7) == '-' && dateTime.charAt(10) == ' ' && dateTime.charAt(13) == ':' && dateTime.charAt(16) == ':') { char a = '-'; char b = ':'; char c = ' '; for (int i = 0; i < dateTime.length(); i++) { if (dateTime.charAt(i) != a && dateTime.charAt(i) != b && dateTime.charAt(i) != c) { deleteString += dateTime.charAt(i); } } parse = LocalDateTime.parse(deleteString, dtf); return parse; } return parse; } }