java基于时间生成的UUID

kingschan 2017-02-10 11:05:58 阅读 1592 评论 0 喜欢 1
uuid 

有4种不同的基本 UUID 类型:

1、基于时间的 UUID;

2、DCE 安全 UUID;

3、基于名称的 UUID;

4、随机生成的 UUID。jdk自带的只有随机的UUID,Java Uuid Generator (JUG) is a library for generating all (3) types of UUIDs on Java除安全UUID


接下来介绍用java基于时间timestamp生成UUID,我是用第三方开源实现的

java-uuid-generator

官网:https://github.com/cowtowncoder/java-uuid-generator/

maven:https://mvnrepository.com/artifact/com.fasterxml.uuid/java-uuid-generator



<dependency>
  <groupId>com.fasterxml.uuid</groupId>
  <artifactId>java-uuid-generator</artifactId>
  <version>3.1.3</version>
</dependency>


java代码:

public static UUID  getUUIDByTimeBase()
{
    return Generators.timeBasedGenerator(EthernetAddress.fromInterface()).generate();
}
public static void main(String[] args)
{
    for (int i = 0; i < 20; i++)
    {
        UUID uuid = BlogUtil.getUUIDByTimeBase();
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(uuid.timestamp() / 1000000 * 100);
        cal.add(Calendar.YEAR, 1582 - 1970);
        cal.add(Calendar.MONTH, 10);
        cal.add(Calendar.DAY_OF_MONTH, 15);
        //输出2017-03-15 10:20:34,其实当前系统时间是2017-02-10 10:20:34 时间上还是有误差
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
        System.out.println(String.format("%s|%s|%s", uuid.toString(), uuid.timestamp(), time));
    }
}

输出:

811f23b8-ef37-11e6-8997-708bcdabbd1a|137059860349920184|2017-03-15 10:20:34
812f2949-ef37-11e6-8997-708bcdabbd1a|137059860350970185|2017-03-15 10:20:35
813bd37a-ef37-11e6-8997-708bcdabbd1a|137059860351800186|2017-03-15 10:20:35
81482f8b-ef37-11e6-8997-708bcdabbd1a|137059860352610187|2017-03-15 10:20:35
815500cc-ef37-11e6-8997-708bcdabbd1a|137059860353450188|2017-03-15 10:20:35
81615cdd-ef37-11e6-8997-708bcdabbd1a|137059860354260189|2017-03-15 10:20:35
816d91de-ef37-11e6-8997-708bcdabbd1a|137059860355060190|2017-03-15 10:20:35
817978bf-ef37-11e6-8997-708bcdabbd1a|137059860355840191|2017-03-15 10:20:35
8185adc0-ef37-11e6-8997-708bcdabbd1a|137059860356640192|2017-03-15 10:20:35
81916d91-ef37-11e6-8997-708bcdabbd1a|137059860357410193|2017-03-15 10:20:35
819d7b82-ef37-11e6-8997-708bcdabbd1a|137059860358200194|2017-03-15 10:20:35
81a96263-ef37-11e6-8997-708bcdabbd1a|137059860358980195|2017-03-15 10:20:35
81b54944-ef37-11e6-8997-708bcdabbd1a|137059860359760196|2017-03-15 10:20:35
81c13025-ef37-11e6-8997-708bcdabbd1a|137059860360540197|2017-03-15 10:20:36
81cd3e16-ef37-11e6-8997-708bcdabbd1a|137059860361330198|2017-03-15 10:20:36
81d924f7-ef37-11e6-8997-708bcdabbd1a|137059860362110199|2017-03-15 10:20:36
81e50bd8-ef37-11e6-8997-708bcdabbd1a|137059860362890200|2017-03-15 10:20:36
81f119c9-ef37-11e6-8997-708bcdabbd1a|137059860363680201|2017-03-15 10:20:36
81fd00aa-ef37-11e6-8997-708bcdabbd1a|137059860364460202|2017-03-15 10:20:36
82090e9b-ef37-11e6-8997-708bcdabbd1a|137059860365250203|2017-03-15 10:20:36


好处:

JDK自带是生成随机UUID这样的话没有办法对数据进行排序,换成基于时间的话就可以解决这一问题了

支持@用户名回复

×  

:bowtie: :smile: :laughing: :blush: :smiley: :relaxed: :smirk: :heart_eyes: :kissing_heart: :kissing_closed_eyes: :flushed: :relieved: :satisfied: :grin: :wink: :stuck_out_tongue_winking_eye: :stuck_out_tongue_closed_eyes: :grinning: :kissing: :kissing_smiling_eyes: :stuck_out_tongue: :sleeping: :worried: :frowning: :anguished: :open_mouth: :grimacing: :confused: :hushed: :expressionless: :unamused: :sweat_smile: :sweat: :weary: :pensive: :disappointed: :confounded: :fearful: :cold_sweat: :persevere: :cry: :sob: :joy: :astonished: :scream: :neckbeard: :tired_face: :angry: :rage: :triumph: :sleepy: :yum: :mask: :sunglasses: :dizzy_face: :imp: :smiling_imp: :neutral_face: :no_mouth: :innocent: :alien: :yellow_heart: :blue_heart: :purple_heart: :heart: :green_heart: :broken_heart: :heartbeat: :heartpulse: :two_hearts: :revolving_hearts: :cupid: :sparkling_heart: :sparkles: :star: :star2: :dizzy: :boom: :collision: :anger: :exclamation: :question: :grey_exclamation: :grey_question: :zzz: :dash: :sweat_drops: :notes: :musical_note: :fire: :hankey: :poop: :shit: :+1: :thumbsup: :-1: :thumbsdown: :ok_hand: :punch: :facepunch: :fist: :v: :wave: :hand: :open_hands: :point_up: :point_down: :point_left: :point_right: :raised_hands: :pray: :point_up_2: :clap: :muscle: :metal: :walking: :runner: :running: :couple: :family: :two_men_holding_hands: :two_women_holding_hands: :dancer: :dancers: :ok_woman: :no_good: :information_desk_person: :raised_hand: :bride_with_veil: :person_with_pouting_face: :person_frowning: :bow: :couplekiss: :couple_with_heart: :massage: :haircut: :nail_care: :boy: :girl: :woman: :man: :baby: :older_woman: :older_man: :person_with_blond_hair: :man_with_gua_pi_mao: :man_with_turban: :construction_worker: :cop: :angel: :princess: :smiley_cat: :smile_cat: :heart_eyes_cat: :kissing_cat: :smirk_cat: :scream_cat: :crying_cat_face: :joy_cat: :pouting_cat: :japanese_ogre: :japanese_goblin: :see_no_evil: :hear_no_evil: :speak_no_evil: :guardsman: :skull: :feet: :lips: :kiss: :droplet: :ear: :eyes: :nose: :tongue: :love_letter: :bust_in_silhouette: :busts_in_silhouette: :speech_balloon: :thought_balloon:

共0条评论

文章导航

  1. java-uuid-generator
  2. java代码:
  3. 输出:
  4. 好处:

相似文章

  • 顶部
  • 关灯
  • 留言
  • 点赞