标签:
time.c的Java实现
public class GMT { public static final int EPOCH_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 long MSECS_DAY = 1000*3600*24L; private long timestamp; private int mil; private int sec; private int min; private int hour; private int wday; private int mday; private int yday; private int mon; private int year; public GMT(long timestamp, long shift) { this.timestamp = timestamp + shift; long dayclock = (this.timestamp % MSECS_DAY) / 1000L; long dayno = 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 long toLongInteger() { return year * 10000000000000L + mon * 100000000000L + mday * 1000000000L + hour * 10000000L + min * 100000L + sec * 1000L + mil; } private static int yearDays(int year) { return leapYear(year) ? 366 : 365; } private static boolean leapYear(int year) { return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); } public int getMil() { return mil; } public void setMil(int mil) { this.mil = mil; } public int getSec() { return sec; } public void setSec(int sec) { this.sec = sec; } public int getMin() { return min; } public void setMin(int min) { this.min = min; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public int getWday() { return wday; } public void setWday(int wday) { this.wday = wday; } public int getMday() { return mday; } public void setMday(int mday) { this.mday = mday; } public int getYday() { return yday; } public void setYday(int yday) { this.yday = yday; } public int getMon() { return mon; } public void setMon(int mon) { this.mon = mon; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public static void main(String[] args) { long start = System.currentTimeMillis(); int total = 500000; for (int i = 0; i < total; i ++) { GMT gmt = new GMT(System.currentTimeMillis(), 1000L*3600*8); System.out.println(gmt.toLongInteger()); } long duration = System.currentTimeMillis() - start; System.out.println("Total: " + duration + "ms, " + total/duration + "/ms"); } }
可以作为Snowflake ID generator的前缀生成器, 好处是易于业务手工识别, 缺点是速度较慢, 与直接使用二进制的机制差一个数量级(sf默认实现是20k/ms, 这个只有2k/ms).
在带I/O的情况下, 能达到150/ms的生成速度, 比使用SimpleDateFormat的效率高很多, 还是可以使用的, 如果使用SimpleDateFormat的话, 只有30/ms的速度.
time.c 的Java实现(从timestamp计算年月日时分秒等数值)
标签:
原文地址:http://www.cnblogs.com/milton/p/5548231.html