java: timestamp -> datetime
This commit is contained in:
parent
bcd520ef12
commit
d4e575352d
108
java_datastamp2date/datetime.java
Normal file
108
java_datastamp2date/datetime.java
Normal file
@ -0,0 +1,108 @@
|
||||
package datetime;
|
||||
|
||||
class DaysOfYear {
|
||||
int year;
|
||||
int days;
|
||||
|
||||
public DaysOfYear(int year, int days) {
|
||||
this.year = year;
|
||||
this.days = days;
|
||||
}
|
||||
}
|
||||
|
||||
class DaysOf4Year {
|
||||
int firstYear;
|
||||
DaysOfYear[] doy;
|
||||
|
||||
void setYearAfter2000(boolean after2k) {
|
||||
this.doy = new DaysOfYear[4];
|
||||
this.doy[0] = new DaysOfYear(4, 1461);
|
||||
this.doy[1] = new DaysOfYear(3, 1096);
|
||||
if (after2k) {
|
||||
this.firstYear = 2000;
|
||||
this.doy[1] = new DaysOfYear(2, 731);
|
||||
this.doy[1] = new DaysOfYear(1, 366);
|
||||
} else {
|
||||
this.firstYear = 1970;
|
||||
this.doy[1] = new DaysOfYear(2, 730);
|
||||
this.doy[1] = new DaysOfYear(1, 365);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Datetime {
|
||||
int year;
|
||||
int month;
|
||||
int date;
|
||||
int hour;
|
||||
int minute;
|
||||
int second;
|
||||
|
||||
private long setYear(long day) {
|
||||
int[] daysOfYear = { 0, 365, 730, 1096 };
|
||||
|
||||
if (day >= 10957) {
|
||||
day -= 10957;
|
||||
this.year = 2000;
|
||||
daysOfYear[1] += 1;
|
||||
daysOfYear[2] += 1;
|
||||
} else {
|
||||
this.year = 1970;
|
||||
}
|
||||
|
||||
while (day >= 1461) {
|
||||
this.year += 4;
|
||||
day -= 1461;
|
||||
}
|
||||
|
||||
for (int i = 3; i > 0; i--) {
|
||||
if (day >= daysOfYear[i]) {
|
||||
this.year += i;
|
||||
day -= daysOfYear[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
private void setMonthDate(int day) {
|
||||
int[] daysBeforeMonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
||||
if ((this.year % 400 == 0) || ((this.year % 100 != 0) && (this.year % 4 == 0))) {
|
||||
for (int i = 2; i < 12; i++) {
|
||||
daysBeforeMonth[i] += 1;
|
||||
}
|
||||
}
|
||||
for (int i = 11; i >= 0; i--) {
|
||||
if (daysBeforeMonth[i] < day) {
|
||||
this.month = i + 1;
|
||||
this.date = day - daysBeforeMonth[i] + 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setSecond(long second) {
|
||||
this.hour = (int)(second / 3600);
|
||||
second %= 3600;
|
||||
this.minute = (int)(second / 60);
|
||||
second %= 60;
|
||||
this.second = (int)second;
|
||||
}
|
||||
|
||||
void loadStamp(long stamp) {
|
||||
this.year = 0;
|
||||
this.month = 0;
|
||||
this.date = 0;
|
||||
this.hour = 0;
|
||||
this.minute = 0;
|
||||
this.second = 0;
|
||||
this.setMonthDate((int)this.setYear(stamp / 86400));
|
||||
this.setSecond(stamp % 86400);
|
||||
}
|
||||
|
||||
String get() {
|
||||
return String.format("%d-%d-%d %d:%02d:%02d",
|
||||
this.year, this.month, this.date, this.hour, this.minute, this.second);
|
||||
}
|
||||
}
|
13
java_datastamp2date/run.java
Normal file
13
java_datastamp2date/run.java
Normal file
@ -0,0 +1,13 @@
|
||||
package datetime;
|
||||
|
||||
public class run {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Datetime curTime = new Datetime();
|
||||
curTime.loadStamp(1550684282);
|
||||
System.out.println(curTime.get());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user