/** * 获取当前指定格式时间 * * @param pattern 格式 * @return 结果 */ public static String getCurrentFormatTime(String pattern) { return formatTime(pattern, getCurrentTimeMillis()); }
/** * 字符串时间转date * * @param time 字符串时间 * @param pattern 时间格式 * @return date * @throws ParseException 装换异常 */ public static Date parseTime(String time, String pattern) throws ParseException { SimpleDateFormat sdf = getDateFormat(pattern); return sdf.parse(time); }
public static Date formatTimeStamp(int date) { return formatTime(date * 1000L); }
/** * @return 当前10位时间戳 */ public static int getCurrentShortTimeMillis() { return (int) (getCurrentTimeMillis() / 1000L); }
/** * 获取当前日期相关int * * @param pattern 需要的格式 * @return int */ public static int getCurrentFormatTimeInt(String pattern) { return StringUtil.parseInt(getCurrentFormatTime(pattern), -1); }
@Override public JSONObject parseInfo(String id) { id = Long.toBinaryString(Long.parseLong(id)); int len = id.length(); JSONObject jsonObject = new JSONObject(); int sequenceStart = len < workerIdShift ? 0 : len - workerIdShift; int workerStart = len < dataCenterIdShift ? 0 : len - dataCenterIdShift; int timeStart = len < timestampLeftShift ? 0 : len - timestampLeftShift; String sequence = id.substring(sequenceStart, len); String workerId = sequenceStart == 0 ? "0" : id.substring(workerStart, sequenceStart); String dataCenterId = workerStart == 0 ? "0" : id.substring(timeStart, workerStart); String time = timeStart == 0 ? "0" : id.substring(0, timeStart); int sequenceInt = Integer.valueOf(sequence, 2); jsonObject.put("sequence", sequenceInt); int workerIdInt = Integer.valueOf(workerId, 2); jsonObject.put("workerId", workerIdInt); int dataCenterIdInt = Integer.valueOf(dataCenterId, 2); jsonObject.put("dataCenter", dataCenterIdInt); long diffTime = Long.parseLong(time, 2); long timeLong = diffTime + startTime; String date = DateUtil.formatTime(null, timeLong); jsonObject.put("date", date); return jsonObject; } }
@Override public synchronized String nextId() { //获取当前毫秒数 long timestamp = timeGen(); //如果服务器时间有问题(时钟后退) 报错。 if (timestamp < lastTimestamp) { throw new RuntimeException(String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果上次生成时间和当前时间相同,在同一毫秒内 if (lastTimestamp == timestamp) { //sequence自增,因为sequence只有12bit,所以和sequenceMask相与一下,去掉高位 sequence = (sequence + 1) & sequenceMask; //判断是否溢出,也就是每毫秒内超过4095,当为4096时,与sequenceMask相与,sequence就等于0 if (sequence == 0) { //自旋等待到下一毫秒 timestamp = tilNextMillis(lastTimestamp); } } else { //如果和上次生成时间不同,重置sequence,就是下一毫秒开始,sequence计数重新从0开始累加 sequence = 0L; } lastTimestamp = timestamp; long suffix = (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; String datePrefix = DateUtil.formatTime("yyyyMMddHHmmssSSS", timestamp); return datePrefix + suffix; }
/** * 根据日期 找到对应日期的 星期 */ public static String getDayOfWeekByDate(String date) throws ParseException { SimpleDateFormat myFormatter = getDateFormat("yyyy-MM-dd");// new SimpleDateFormat("yyyy-MM-dd"); Date myDate = myFormatter.parse(date); SimpleDateFormat formatter = getDateFormat("E");// new SimpleDateFormat("E"); return formatter.format(myDate); } }
/** * 格式化13位时间戳 * * @param pattern 格式 * @param timeMillis 13位时间戳 * @return 结果 * @author jiangzeyin */ public static String formatTime(String pattern, long timeMillis) { if (pattern == null || pattern.length() == 0) pattern = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = getDateFormat(pattern); return sdf.format(timeMillis); }
/** * 格式化10位时间戳 * * @param pattern 格式 * @param timeMillis 10位时间戳 * @return 结果 * @author jiangzeyin */ public static String formatTimeStamp(String pattern, int timeMillis) { if (pattern == null || pattern.length() == 0) pattern = "yyyy-MM-dd HH:mm:ss"; Calendar nowDate = new GregorianCalendar(); nowDate.setTimeInMillis(timeMillis * 1000L); SimpleDateFormat df = getDateFormat(pattern); return df.format(nowDate.getTime()); }