Skip to content

Instantly share code, notes, and snippets.

@luqinghui
Last active September 30, 2016 08:21
Show Gist options
  • Save luqinghui/b54516fef3acd29dfe1090b83e82335a to your computer and use it in GitHub Desktop.
Save luqinghui/b54516fef3acd29dfe1090b83e82335a to your computer and use it in GitHub Desktop.
public class Date_My {
private final int value;
public Date_My(int year, int month, int day){
value = year*512 + month*32 + day;
// value = y * 2^9 + m * 2^5+ d;
}
public int month(){
return (value / 32) % 16;
// 除以32后,剩下 y*2^4 + m(d消掉了,2^4 = 2^9 / 2^5),接着再用16(即2^4)取余,得month
}
public int day(){
return value % 32;
// 用32取余自然得到余数day
}
public int year(){
return value / 512;
// 用512取余自然得到余数year
}
public String toString(){
return month() + "/" + day() + "/" +year();
}
public static void main(String args[]){
Date_My date = new Date_My(2015, 10, 15);
StdOut.println(date.year());
StdOut.println(date.month());
StdOut.println(date.day());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment