com.beginner.core.utils.UUIDUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.UUIDUtil.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beginner.core.utils;

import java.util.UUID;

import org.joda.time.DateTime;

/**
 * <b>??</b>UUIDUtil<br/>
 * <b>??</b>UUID?<br/>
 * <b></b>Hsiao Lin Studio<br/>
 * <b></b><br/>
 * <b></b>20150521 ?6:18:18<br/>
 * <b></b><br/>
 * @version 1.0.0<br/>
 */
public class UUIDUtil {

    private static long num = 0;

    /**
     * ??36?UUID(?)
     * @return    36?UUID<br>6a6cd388-f7ce-459c-a2e0-3747498c6786
     */
    public static synchronized String get36UUID() {
        return UUID.randomUUID().toString().trim();
    }

    /**
     * ??36?UUID()
     * @return    36?UUID<br>6A6CD388-F7CE-459C-A2E0-3747498C6786
     */
    public static synchronized String get36UpperUUID() {
        return get36UUID().toUpperCase();
    }

    /**
     * ???32?UUID(?)
     * @return    32?UUID<br>5dd5e786d748438fb906ddf1c32be594
     */
    public static synchronized String get32UUID() {
        return get36UUID().replaceAll("-", "");
    }

    /**
     * ???32?UUID()
     * @return    32?UUID<br>5DD5E786D748438FB906DDF1C32BE594
     */
    public static synchronized String get32UpperUUID() {
        return get32UUID().toUpperCase();
    }

    /**
     * ??UUID
     * @return    32?UUID<br>Beginner2ddabc33a6103ceb9311697534035d8d
     */
    public static synchronized String getUUID(String name) {
        return UUID.nameUUIDFromBytes(name.getBytes()).toString().trim().replaceAll("-", "");
    }

    /**
      * ???ID
      * @return <br>
      * 20160219133512314<br>
      * 2016021913351231401<br>
      */
    public static synchronized long getLongId() {
        String id = DateTime.now().toString("yyyyMMddHHmmssS");
        if (num >= 99)
            num = 0l;
        ++num;
        if (num < 10)
            id = id + 00 + num;
        else
            id += num;
        return Long.valueOf(id);
    }
}