Advertisement
Guest User

Untitled

a guest
May 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. package de.ostfalia.gdp.ss18;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. *
  7. * @author Raffael, Moritz
  8. *
  9. */
  10.  
  11. public class DateTimeBi {
  12. public static void main (String[]args) {
  13.  
  14. }
  15. /**
  16. * umrechnung in millisekunden + deklaration von statischen variablen
  17. */
  18. private static final String[] DAYNAME = { "Merkur", "Venus", "Erde", "Mars", "Jupiter", "Saturn", "Uranus", "Neptunpisser" };
  19. private static final long MILLISEC = 1000;
  20. private static final long SECBI = 64; // sekunden in bi
  21. private static final long MIN_SEC = 64; // minuten in bi
  22. private static final long DAYBI = 16; // in bi
  23. private static final long DAYS_IN_MONTHBI = 32; // tage eines monats in bi
  24. private static final long HOUR_SEC = SECBI * MIN_SEC;
  25. private static final long DAY_SEC = DAYBI * HOUR_SEC;
  26. private static final long MONTH_SEC = DAYS_IN_MONTHBI * DAY_SEC;
  27. private static final long YEAR_IN_SEC = DAYBI * MONTH_SEC;
  28.  
  29. private static final long YEARMILLI = MILLISEC * YEAR_IN_SEC; // für DATE
  30. private static final long MONTHMILLI = MILLISEC * MONTH_SEC;
  31. private static final long DAYMILLI = MILLISEC * DAY_SEC;
  32. private static final long HOURMILLI = MILLISEC * SECBI * MIN_SEC;
  33. private static final long MINMILLI = MILLISEC * SECBI;
  34. private static final long WEEKMILLI = DAYMILLI * 8;
  35.  
  36. private long milliseconds;
  37.  
  38. private static final long UNIX_TIME_TO_NOW = 275248380000L;
  39.  
  40. /**
  41. * berechnung der Differenz in Zeit zwischen 1960/1970
  42. */
  43. public DateTimeBi() {
  44. this.milliseconds = System.currentTimeMillis() + UNIX_TIME_TO_NOW;
  45. }
  46.  
  47. /**
  48. * hier wird die Gesamtzeit der eingegebenen Werte in Millis wird berechnet
  49. *
  50. * @param dayBi
  51. * tag
  52. * @param monthBi
  53. * monat
  54. * @param yearBi
  55. * jahr
  56. * @param hourBi
  57. * stunde
  58. * @param minuteBi
  59. * minute
  60. */
  61. public DateTimeBi(int dayBi, int monthBi, int yearBi, int hourBi, int minuteBi) {
  62. long allTime = yearBi * YEARMILLI + monthBi * MONTHMILLI + dayBi * DAYMILLI + hourBi * HOURMILLI
  63. + minuteBi * MINMILLI;
  64. this.milliseconds = allTime;
  65. }
  66.  
  67. /**
  68. * Hilfsmethode
  69. *
  70. * @return exakter monat wird berechnet, nachkommastellen werden abgeschnitten.
  71. * Jahre werden abgezogen
  72. */
  73. public int getMonth() {
  74. long withoutYear = (this.milliseconds - getYear() * YEARMILLI);
  75. return ((int) (withoutYear / MONTHMILLI));
  76. }
  77.  
  78. /**
  79. * Hilfsmethode
  80. *
  81. * @return exakte woche wird berechnet, nachkommastellen werden abgeschnitten.
  82. * Jahre und Monate werden abgezogen
  83. */
  84. public int getWeeks() {
  85. long withoutMonthandYEAR = (this.milliseconds - getMonth() * MONTHMILLI - getYear() * YEARMILLI);
  86. return ((int) (withoutMonthandYEAR / WEEKMILLI));
  87. }
  88.  
  89. /**
  90. * Hilfsmethode
  91. *
  92. * @return exaktes Jahr wird berechnet
  93. */
  94. public int getYear() {
  95. return ((int) (this.milliseconds / YEARMILLI));
  96. }
  97.  
  98. /**
  99. * Hilfsmethode
  100. *
  101. * @return exakter Tag wird berechnet, monate und jahre werden abgezogen
  102. */
  103. public int getDay() { // tage eines Monats im Prinzip modulo
  104. long withoutMonthandYEAR = (this.milliseconds - getMonth() * MONTHMILLI - getYear() * YEARMILLI);
  105. return ((int) (withoutMonthandYEAR / DAYMILLI));
  106. }
  107.  
  108. // erzeugt ein neues Object mit dem selben Tag wie day, aber HourBi und MinBi
  109. // neu gesetzt
  110. /**
  111. * "roher" Tag wird berechnet und hourBi und minBi werden hinzugefügt
  112. *
  113. * @param day
  114. * aus vorherigen Methoden übernommen
  115. * @param hourBi
  116. * eingegebene Stunden
  117. * @param minBi
  118. * eingegebene Minuten
  119. */
  120. public DateTimeBi(DateTimeBi day, int hourBi, int minBi) {
  121. long newRawDay = day.milliseconds;
  122. newRawDay = day.getYear() * YEARMILLI + day.getMonth() * MONTHMILLI + day.getDay() * DAYMILLI; // neuer Tag um
  123. // 00:00
  124. // this.milliseconds = (
  125. // MILLISEC * SEK_PER_MINBI * MIN_PER_HOURBI * HOURS_PER_DAYBI);
  126. //
  127. // long milliseconds = (hourBi * MIN_PER_HOURBI) + minBi; // stunden neu
  128. // berechnet
  129. // milliseconds *= SEK_PER_MINBI * MILLISEC;
  130. this.milliseconds = newRawDay + hourBi * HOURMILLI + minBi * MINMILLI;
  131. }
  132.  
  133. // public static DateTimeBi today(int hourBi, int minBi) {
  134. // DateTimeBi a = new DateTimeBi();
  135. //
  136. // return new DateTimeBi(a, hourBi, minBi);
  137. // }
  138. /**
  139. *
  140. * @param hourBi
  141. * wert
  142. * @param minBi
  143. * wert
  144. * @return heutiger Tag mit neuer Stunde und neuen Minuten
  145. */
  146. public static DateTimeBi today(int hourBi, int minBi) {
  147. DateTimeBi toDaynow = new DateTimeBi();
  148. return new DateTimeBi(toDaynow, hourBi, minBi);
  149. }
  150.  
  151. /**
  152. * Hilfsmethode
  153. *
  154. * @param milliseconds
  155. * werden zurückgesetzt
  156. */
  157. private DateTimeBi(long milliseconds) {
  158. this.milliseconds = milliseconds;
  159. }
  160.  
  161. /**
  162. *
  163. * @param number
  164. * wert
  165. * @return Datum + anzahl der Tage (number)
  166. */
  167. public DateTimeBi daysBiLater(int number) {
  168. long newMILLISEC = this.milliseconds;
  169. newMILLISEC += (number * DAYMILLI);
  170. return new DateTimeBi(newMILLISEC);
  171. }
  172.  
  173. /**
  174. * Datum als String liefern (hexadezimal DayBi.MonthBi.YearBi),
  175. *
  176. * @return datum als string
  177. */
  178. public String getDate() {
  179. return Integer.toHexString(getDay()) + "." + Integer.toHexString(getMonth()) + "."
  180. + Integer.toHexString(getYear());
  181. }
  182.  
  183. /**
  184. * zeit wird als hexa zurück gegeben
  185. *
  186. * @return zeit
  187. */
  188. public String getTime() {
  189. long rawTimeMilli = (this.milliseconds
  190. - (getYear() * YEARMILLI + getMonth() * MONTHMILLI + getDay() * DAYMILLI));
  191. int hours = (int) (rawTimeMilli / HOURMILLI);
  192. int mins = (int) ((rawTimeMilli - hours * HOURMILLI) / MINMILLI);
  193. // return Integer.toHexString(hours) + ":" + Integer.toHexString(mins);
  194. return String.format("%02x:%02x", hours, mins);
  195. }
  196.  
  197. /**
  198. *
  199. * @return wochentag als Zahl
  200. */
  201. public int dayOfWeekInt() {
  202. int weekday = (getDay()) % 8;
  203. return weekday;
  204. }
  205.  
  206. /**
  207. *
  208. * @return wochentag als string + "Tag"
  209. */
  210. public String dayOfWeek() {
  211. return DAYNAME[dayOfWeekInt()] + "Tag";
  212. }
  213.  
  214. /**
  215. * differenz berechnen
  216. *
  217. * @param start
  218. * bestimmtes Datum
  219. * @return differenz zwischen jetzt und Datum (start)
  220. */
  221. public long getDuration(DateTimeBi start) {
  222. long diff = Math.abs(this.milliseconds - start.milliseconds);
  223. return diff;
  224. }
  225.  
  226. /**
  227. * Datum, Uhrzeit und Tag sollen ausgegeben werden
  228. *
  229. * @param start
  230. * bestimmtes Datum
  231. * @return alles wird als String ausgegeben
  232. */
  233. public String getDurationString(DateTimeBi start) {
  234. DateTimeBi diffy = new DateTimeBi(getDuration(start));
  235. int years = diffy.getYear();
  236. int months = diffy.getMonth();
  237. int weeks = diffy.getWeeks();
  238. int days = diffy.dayOfWeekInt();// + 1
  239. String time = diffy.getTime();
  240.  
  241. StringBuffer duration = new StringBuffer();
  242. if (years > 0) {
  243. duration.append(years + " YearsBi ");// append zusammenfügen
  244. }
  245.  
  246. if (months > 0) {
  247. duration.append(months + " MonthsBi ");
  248. }
  249.  
  250. if (weeks > 0) {
  251. duration.append(weeks + " WeekBi ");
  252. }
  253.  
  254. if (days > 0) {
  255. duration.append(days + " DayBi ");
  256. }
  257.  
  258. // if (hours > 0)
  259. // duration.append(hours + ":");
  260. duration.append(time);
  261.  
  262. return duration.toString();
  263.  
  264. }
  265.  
  266. /**
  267. * String wird überschrieben
  268. *
  269. * @return zeug
  270. */
  271. public String toString() {
  272. return dayOfWeek() + ", " + getDate() + " " + getTime();
  273. }
  274.  
  275. /**
  276. *
  277. * @param other
  278. * zweite Zeit
  279. * @return true/ false, ob danach
  280. */
  281. public boolean isAfter(DateTimeBi other) {
  282. return (milliseconds >= other.milliseconds);
  283. }
  284.  
  285. /**
  286. *
  287. * @param other
  288. * zweite Zeit
  289. * @return true/ false, ob davor
  290. */
  291. public boolean isBefore(DateTimeBi other) {
  292. return (milliseconds <= other.milliseconds);
  293. }
  294.  
  295. /**
  296. * wie oben, nur wird jetzt lediglich noch auf den Tag/ Stunden/ etc. geschaut
  297. *
  298. * @param other
  299. * anderes Datum
  300. * @param ignoreWeek
  301. * woche soll außen vor gelassen werden
  302. * @return true/ false
  303. */
  304. public boolean isAfter(DateTimeBi other, boolean ignoreWeek) {
  305. long millinew = milliseconds;
  306. long othermilli = other.milliseconds;
  307. if (ignoreWeek) {
  308. millinew -= getYear() * YEARMILLI + getMonth() * MONTHMILLI + getWeeks() * WEEKMILLI;
  309. othermilli -= other.getYear() * YEARMILLI + other.getMonth() * MONTHMILLI + other.getWeeks() * WEEKMILLI;
  310. }
  311. return (millinew >= othermilli);
  312. }
  313.  
  314. /**
  315. * wie oben, nur wird jetzt lediglich noch auf den Tag/ Stunden/ etc. geschaut
  316. *
  317. * @param other
  318. * anderes Datum
  319. * @param ignoreWeek
  320. * woche soll außen vor gelassen werden
  321. * @return true/ false
  322. */
  323. public boolean isBefore(DateTimeBi other, boolean ignoreWeek) {
  324. long millinew = milliseconds;
  325. long othermilli = other.milliseconds;
  326. if (ignoreWeek) {
  327. millinew -= getYear() * YEARMILLI + getMonth() * MONTHMILLI + getWeeks() * WEEKMILLI;
  328. othermilli -= other.getYear() * YEARMILLI + other.getMonth() * MONTHMILLI + other.getWeeks() * WEEKMILLI;
  329. }
  330. // if (milliseconds < other.milliseconds) {
  331. // return true;
  332. // }else if(milliseconds == other.milliseconds){
  333. // return true;
  334. // }else {
  335. // return false;
  336. // }
  337.  
  338. return (millinew <= othermilli);
  339. }
  340.  
  341. /**
  342. * kalender in unserer zeit
  343. *
  344. * @param cal
  345. * kalender
  346. */
  347. public DateTimeBi(Calendar cal) {
  348. this.milliseconds = cal.getTimeInMillis() + UNIX_TIME_TO_NOW;
  349. }
  350.  
  351. /**
  352. *
  353. * @return erdzeit im kalender im vergleich zu weltraumzeit
  354. */
  355. public Calendar earthTime() {
  356. Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  357. cal.setTimeInMillis(milliseconds - UNIX_TIME_TO_NOW);
  358. return cal;
  359. }
  360.  
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement