Java implementation of TIME.C
Public classGMT { Public Static Final intepoch_year = 1970; Public Static Final int[] Month_days = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; Public Static Final LongMsecs_day = 1000*3600*24l; Private Longtimestamp; Private intmil; Private intsec; Private intmin; Private inthour; Private intwday; Private intMday; Private intyday; Private intMon; Private intYear ; PublicGMT (LongTimestampLongshift) { This. Timestamp = timestamp +shift; LongDayclock = ( This. Timestamp% msecs_day)/1000L; LongDayno = This. Timestamp/Msecs_day; Mil= (int) ( This. Timestamp% 1000L); SEC= (int) (Dayclock% 60); Min= (int) ((dayclock% 3600)/60); Hour= (int) (dayclock/3600); Wday= (int) ((Dayno + 4)% 7); while(Dayno >= yeardays (epoch_year +Year )) {Dayno-= Yeardays (Epoch_year +Year ); year++; } Year= Epoch_year +Year ; Yday= (int) Dayno; int[] monthdays = Leapyear (year)? MONTH_DAYS[1]: month_days[0]; while(Dayno >=Monthdays[mon]) {Dayno-=Monthdays[mon]; Mon++; } Mon++; Mday= (int) Dayno + 1; } Public LongTolonginteger () {returnYear * 10000000000000L + Mon * 100000000000L + mday * 1000000000L + Ho ur * 10000000L + min * 100000L + sec * 1000L +mil; } Private Static intYeardays (intYear ) { returnLeapyear (year)? 366:365; } Private Static BooleanLeapyear (intYear ) { return(year% 4 = = 0) && (year% 100! = 0)) | | (Year% 400 = = 0); } Public intGetmil () {returnMil;} Public voidSetmil (intMIL) { This. mil =Mil;} Public intGetsec () {returnsec;} Public voidSetsec (intSEC) { This. SEC =sec;} Public intGetmin () {returnmin;} Public voidSetmin (intMin) { This. Min =min;} Public intGethour () {returnHour;} Public voidSethour (intHour) { This. Hour =Hour;} Public intGetwday () {returnWday;} Public voidSetwday (intWday) { This. wday =Wday;} Public intGetmday () {returnMday;} Public voidSetmday (intMday) { This. Mday =Mday;} Public intGetyday () {returnYday;} Public voidSetyday (intYday) { This. Yday =Yday;} Public intGetmon () {returnMon;} Public voidSetmon (intMon) { This. Mon =Mon;} Public intGetYear () {returnYear ;} Public voidSetyear (intYear) { This. Year =Year ;} Public Static voidMain (string[] args) {LongStart =System.currenttimemillis (); intTotal = 500000; for(inti = 0; I < total; i + +) {GMT GMT=NewGMT (System.currenttimemillis (), 1000l*3600*8); System.out.println (Gmt.tolonginteger ()); } LongDuration = System.currenttimemillis ()-start; System.out.println ("Total:" + duration + "MS," + total/duration + "/ms"); }}
Can be used as the prefix generator for Snowflake ID generator, the advantage is that it is easy to manually identify the business, the disadvantage is that the speed is slower, and the direct use of binary mechanism of an order of magnitude (SF default implementation is 20k/ms, this only 2k/ms).
In the case of with I/O, can achieve 150/ms speed, more efficient than the use of SimpleDateFormat, or can be used, if the use of SimpleDateFormat, only 30/ms speed.
Java implementation of TIME.C (calculated from timestamp, minutes, minutes and seconds)