PageRenderTime 170ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/date/DateConverter.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 623 lines | 338 code | 54 blank | 231 comment | 26 complexity | 72041148093d1da43a692ede504588ea MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.date;
  18. import java.text.DateFormat;
  19. import java.text.DateFormatSymbols;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Calendar;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Vector;
  29. import mpv5.db.objects.User;
  30. import mpv5.handler.FormatHandler;
  31. import mpv5.logging.Log;
  32. import mpv5.utils.text.TypeConversion;
  33. /**
  34. *
  35. * This class provides date formatting methods and default date formats
  36. */
  37. public class DateConverter {
  38. /**
  39. * Gets month strings. For example: "January", "February", etc.
  40. */
  41. public static String[] months = DateFormatSymbols.getInstance().getMonths();
  42. private static Calendar cl = Calendar.getInstance();
  43. /**
  44. * The date formatter with the short formatting style for the default locale.
  45. */
  46. public static DateFormat DEF_DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
  47. /**
  48. * "EEE, dd MMM yyyy HH:mm:ss z"
  49. */
  50. public static final DateFormat NATIVE_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
  51. /**
  52. * "dd.MM.yyy - HH:mm:ss"
  53. */
  54. public static final DateFormat DE_FULL_DATE_FORMAT = new SimpleDateFormat("dd.MM.yyy - HH:mm:ss");
  55. /**
  56. * "yyyy-MM-dd HH:mm:ss"
  57. */
  58. public static final DateFormat DB_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  59. /**
  60. * DE format
  61. */
  62. public static final DateFormat DE_DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
  63. /**
  64. * "yyyy-MM-dd"
  65. */
  66. public static final DateFormat ENG_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
  67. /**
  68. * YYYY
  69. */
  70. public static final DateFormat YEAR_DATE_FORMAT = new SimpleDateFormat("yyyy");
  71. /**
  72. * Contains all available default date formats
  73. */
  74. public static final ArrayList<DateFormat> DATE_FORMATS = new ArrayList<DateFormat>(Arrays.asList(new DateFormat[]{
  75. DB_DATE_FORMAT,
  76. ENG_DATE_FORMAT,
  77. DE_DATE_FORMAT,
  78. DE_FULL_DATE_FORMAT,
  79. NATIVE_DATE_FORMAT,
  80. DEF_DATE_FORMAT,
  81. YEAR_DATE_FORMAT
  82. }));
  83. /**
  84. *
  85. * @param date
  86. * @param amount
  87. * @return
  88. */
  89. public static synchronized Date addDays(Date date, Integer amount) {
  90. cl.setTime(date);
  91. cl.add(Calendar.DATE, amount);
  92. return cl.getTime();
  93. }
  94. /**
  95. *
  96. * @param date
  97. * @param amount
  98. * @return
  99. */
  100. public static synchronized Date addDynamic(Date date, Integer amount, Integer Typ) {
  101. cl.setTime(date);
  102. cl.add(Typ, amount);
  103. return cl.getTime();
  104. }
  105. /**
  106. *
  107. * @param date
  108. * @param amount
  109. * @return
  110. */
  111. public static synchronized Date addYears(Date date, int amount) {
  112. cl.setTime(date);
  113. cl.add(Calendar.YEAR, amount);
  114. return cl.getTime();
  115. }
  116. /**
  117. * Get DAYS difference
  118. * @param date1
  119. * @param date2
  120. * @return
  121. */
  122. public static synchronized Integer getDifferenceBetween(Date date1, Date date2) {
  123. if (date1.after(date2)) {
  124. Date swap = date1;
  125. date1 = date2;
  126. date2 = swap;
  127. }
  128. Calendar d1 = Calendar.getInstance();
  129. d1.setTime(date1);
  130. Calendar d2 = Calendar.getInstance();
  131. d2.setTime(date2);
  132. int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
  133. - d1.get(java.util.Calendar.DAY_OF_YEAR);
  134. int y2 = d2.get(java.util.Calendar.YEAR);
  135. if (d1.get(java.util.Calendar.YEAR) != y2) {
  136. d1 = (java.util.Calendar) d1.clone();
  137. do {
  138. days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
  139. d1.add(java.util.Calendar.YEAR, 1);
  140. } while (d1.get(java.util.Calendar.YEAR) != y2);
  141. }
  142. return days;
  143. }
  144. /**
  145. * Returns the same date , one second before the next day
  146. * @param date
  147. * @return
  148. */
  149. public static synchronized Date getEndOfDay(Date date) {
  150. Calendar calendar = cl;
  151. synchronized (calendar) {
  152. calendar.setTime(date);
  153. calendar.set(Calendar.HOUR_OF_DAY, 23);
  154. calendar.set(Calendar.MILLISECOND, 999);
  155. calendar.set(Calendar.SECOND, 59);
  156. calendar.set(Calendar.MINUTE, 59);
  157. return calendar.getTime();
  158. }
  159. }
  160. /**
  161. * Returns the same date, first millisecond
  162. * @param date
  163. * @return
  164. */
  165. public static synchronized Date getStartOfDay(Date date) {
  166. Calendar calendar = cl;
  167. synchronized (calendar) {
  168. calendar.setTime(date);
  169. calendar.set(Calendar.HOUR_OF_DAY, 0);
  170. calendar.set(Calendar.MILLISECOND, 1);
  171. calendar.set(Calendar.SECOND, 0);
  172. calendar.set(Calendar.MINUTE, 0);
  173. return calendar.getTime();
  174. }
  175. }
  176. /**
  177. * Returns the same date, first millisecond of the year
  178. * @param date
  179. * @return
  180. */
  181. public static synchronized Date getStartOfYear(Date date) {
  182. Calendar calendar = cl;
  183. synchronized (calendar) {
  184. calendar.setTime(date);
  185. calendar.set(Calendar.HOUR_OF_DAY, 0);
  186. calendar.set(Calendar.MILLISECOND, 1);
  187. calendar.set(Calendar.SECOND, 0);
  188. calendar.set(Calendar.MINUTE, 0);
  189. calendar.set(Calendar.MONTH, 0);
  190. calendar.set(Calendar.DAY_OF_MONTH, 1);
  191. return calendar.getTime();
  192. }
  193. }
  194. /**
  195. * Returns the same date, last millisecond of the year
  196. * @param date
  197. * @return
  198. */
  199. public static synchronized Date getEndOfYear(Date date) {
  200. Calendar calendar = cl;
  201. synchronized (calendar) {
  202. calendar.setTime(date);
  203. calendar.set(Calendar.HOUR_OF_DAY, 23);
  204. calendar.set(Calendar.MILLISECOND, 999);
  205. calendar.set(Calendar.SECOND, 59);
  206. calendar.set(Calendar.MINUTE, 59);
  207. calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
  208. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  209. return calendar.getTime();
  210. }
  211. }
  212. /**
  213. * Returns the same date, first millisecond
  214. * @param date
  215. * @return
  216. */
  217. public static synchronized Date getStartOfMonth(Date date) {
  218. Calendar calendar = cl;
  219. synchronized (calendar) {
  220. calendar.setTime(date);
  221. calendar.set(Calendar.HOUR_OF_DAY, 0);
  222. calendar.set(Calendar.MILLISECOND, 1);
  223. calendar.set(Calendar.SECOND, 0);
  224. calendar.set(Calendar.MINUTE, 0);
  225. calendar.set(Calendar.DAY_OF_MONTH, 1);
  226. return calendar.getTime();
  227. }
  228. }
  229. /**
  230. * Returns the same date, last millisecond of the month
  231. * @param date
  232. * @return
  233. */
  234. public static synchronized Date getEndOfMonth(Date date) {
  235. Calendar calendar = cl;
  236. synchronized (calendar) {
  237. calendar.setTime(date);
  238. calendar.set(Calendar.HOUR_OF_DAY, 23);
  239. calendar.set(Calendar.MILLISECOND, 999);
  240. calendar.set(Calendar.SECOND, 59);
  241. calendar.set(Calendar.MINUTE, 59);
  242. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  243. return calendar.getTime();
  244. }
  245. }
  246. /**
  247. * Quarter as 1,2,3,4
  248. * @return
  249. */
  250. public static synchronized int getQuarter() {
  251. Calendar cal = Calendar.getInstance();
  252. cal.setTime(new Date());
  253. int month = cal.get(Calendar.MONTH); /* 0 through 11 */
  254. int quarter = (month / 3) + 1;
  255. return quarter;
  256. }
  257. /**
  258. * Quarter of a given date as 1, 2, 3, 4
  259. * @param date
  260. * @return Quarter as 1, 2, 3, 4
  261. */
  262. public static synchronized int getQuarter(Date date) {
  263. Calendar cal = Calendar.getInstance();
  264. cal.setTime(date);
  265. int month = cal.get(Calendar.MONTH); /* 0 through 11 */
  266. int quarter = (month / 3) + 1;
  267. return quarter;
  268. }
  269. /**
  270. * The current date in the default, localized format
  271. * @return
  272. */
  273. public static synchronized String getTodayDefDate() {
  274. return DE_DATE_FORMAT.format(new Date());
  275. }
  276. /**
  277. * The current date in SQL format
  278. * @return
  279. */
  280. public static synchronized String getTodayDBDate() {
  281. return DB_DATE_FORMAT.format(new Date());
  282. }
  283. /**
  284. *
  285. * @param date
  286. * @return
  287. */
  288. public static synchronized Date addYear(Date date) {
  289. return addYears(date, 1);
  290. }
  291. /**
  292. *
  293. * @param date
  294. * @return The next month
  295. */
  296. public static synchronized Date addMonth(Date date) {
  297. return addMonths(date, 1);
  298. }
  299. /**
  300. *
  301. * @param date
  302. * @param amount
  303. * @return The next month
  304. */
  305. public static synchronized Date addMonths(Date date, int amount) {
  306. Calendar cal = DateConverter.cl;
  307. synchronized (cal) {
  308. cal.setTime(date);
  309. cal.add(Calendar.MONTH, amount);
  310. return cal.getTime();
  311. }
  312. }
  313. /**
  314. *
  315. * @param date
  316. * @return End of the quarter
  317. */
  318. public static synchronized Date addQuarter(Date date) {
  319. Calendar cal = DateConverter.cl;
  320. synchronized (cal) {
  321. cal.setTime(date);
  322. cal.add(Calendar.MONTH, 3);
  323. return cal.getTime();
  324. }
  325. }
  326. /**
  327. *
  328. * @param date
  329. * @return The next day
  330. */
  331. public static synchronized Date addDay(Date date) {
  332. return addDays(date, 1);
  333. }
  334. /**
  335. *
  336. * @param date
  337. * @return SQL conform date String
  338. */
  339. public static synchronized String getSQLDateString(Date date) {
  340. return DB_DATE_FORMAT.format(date);
  341. }
  342. /**
  343. *
  344. * @param date
  345. * @return Default date
  346. */
  347. public static synchronized String getDefDateString(Date date) {
  348. return DEF_DATE_FORMAT.format(date);
  349. }
  350. /**
  351. *
  352. * @param datum
  353. * @return
  354. */
  355. public static synchronized String getDay(Date datum) {
  356. return DE_DATE_FORMAT.format(datum);
  357. }
  358. /**
  359. *
  360. * @param date
  361. * @return
  362. */
  363. public static synchronized String getMonth(Date date) {
  364. Calendar cal = Calendar.getInstance();
  365. cal.setTime(date);
  366. return String.valueOf(cal.get(Calendar.MONTH) + 1);
  367. }
  368. /**
  369. *
  370. * @param date
  371. * @return
  372. */
  373. public static synchronized String getMonthName(Date date) {
  374. Calendar cal = Calendar.getInstance();
  375. cal.setTime(date);
  376. return months[cal.get(Calendar.MONTH)];
  377. }
  378. /**
  379. *
  380. * @param date
  381. * @return
  382. */
  383. public static synchronized String getYearName(Date date) {
  384. Calendar cal = Calendar.getInstance();
  385. cal.setTime(date);
  386. return String.valueOf(cal.get(Calendar.YEAR));
  387. }
  388. /**
  389. *
  390. * @param date
  391. * @return
  392. */
  393. public static synchronized String getDayOfMonth(Date date) {
  394. Calendar cal = Calendar.getInstance();
  395. cal.setTime(date);
  396. return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
  397. }
  398. /**
  399. * Converts formated
  400. * Default date (dd.mm.yyyy) or variants
  401. * or
  402. * yyyy-mm-dd hh.mm.ss[.nnnnnn] - SQL DATE Timestamp
  403. *
  404. * to a Date object.
  405. *
  406. * @param date
  407. * @return Parsed date
  408. */
  409. public static synchronized Date getDate(String date) {
  410. Date DATE = null;
  411. for (DateFormat d : DATE_FORMATS) {
  412. try {
  413. DATE = d.parse(date);
  414. return DATE;
  415. } catch (ParseException ex) {
  416. }
  417. }
  418. if (additionalFormats.isEmpty()) {
  419. buildFormats();
  420. }
  421. if (DATE == null) {
  422. for (DateFormat d : additionalFormats) {
  423. try {
  424. DATE = d.parse(date);
  425. return DATE;
  426. } catch (ParseException ex) {
  427. }
  428. }
  429. }
  430. if (DATE == null) {
  431. Log.Debug(DateConverter.class, "String not parseable to a date: " + date);
  432. }
  433. return DATE;
  434. }
  435. static final List<DateFormat> additionalFormats = new Vector<DateFormat>();
  436. /**
  437. * Try to parse the given object to a date
  438. * @param object
  439. * @return
  440. */
  441. public static synchronized Date getDate(Object object) {
  442. if (object instanceof Date) {
  443. return (Date) object;
  444. } else if (object instanceof java.sql.Date) {
  445. return new Date(((java.sql.Date) object).getTime());
  446. } else {
  447. return getDate(object.toString());
  448. }
  449. }
  450. /**
  451. *
  452. * @return
  453. */
  454. public static synchronized String getYear() {
  455. return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
  456. }
  457. /**
  458. *
  459. * @return
  460. */
  461. public static synchronized String getDayOfMonth() {
  462. String m = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
  463. if (m.length() == 1) {
  464. m = "0" + m;
  465. }
  466. return m;
  467. }
  468. /**
  469. *
  470. * @return
  471. */
  472. public static synchronized String getDayMonthAndYear() {
  473. return DateConverter.getDefDateString(new Date());
  474. }
  475. /**
  476. *
  477. * @return
  478. */
  479. public static synchronized String getMonth() {
  480. String m = String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1);
  481. if (m.length() == 1) {
  482. m = "0" + m;
  483. }
  484. return m;
  485. }
  486. /**
  487. *
  488. * @return
  489. */
  490. public static synchronized String getMonthName() {
  491. Calendar cal = Calendar.getInstance();
  492. return months[cal.get(Calendar.MONTH)];
  493. }
  494. /**
  495. *
  496. * @param df
  497. */
  498. public static synchronized void setDefaultFormat(DateFormat df) {
  499. DEF_DATE_FORMAT = df;
  500. }
  501. /**
  502. *
  503. * @return
  504. */
  505. public static synchronized String getDefaultFormatString() {
  506. if (DEF_DATE_FORMAT instanceof SimpleDateFormat) {
  507. return ((SimpleDateFormat) DEF_DATE_FORMAT).toPattern();
  508. } else {
  509. return "dd.MM.yyyy";
  510. }
  511. }
  512. private static synchronized void buildFormats() {
  513. Locale[] locales = DateFormat.getAvailableLocales();
  514. for (int i = 0; i < locales.length; i++) {
  515. Locale locale = locales[i];
  516. additionalFormats.addAll(Arrays.asList(new DateFormat[]{
  517. DateFormat.getDateInstance(DateFormat.SHORT, locale),
  518. DateFormat.getDateInstance(DateFormat.MEDIUM, locale),
  519. DateFormat.getDateInstance(DateFormat.LONG, locale),
  520. DateFormat.getDateInstance(DateFormat.FULL, locale),
  521. DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale),
  522. DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale),
  523. DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale),
  524. DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale),
  525. DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale),
  526. DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale),
  527. DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale),
  528. DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale),
  529. DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale),
  530. DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale),
  531. DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale),
  532. DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale),
  533. DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale),
  534. DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale),
  535. DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale),
  536. DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale)
  537. }));
  538. }
  539. }
  540. /**
  541. * Generates a random date, which is not in the future
  542. * @return
  543. */
  544. public static Date getRandomDate() {
  545. return new RandomDate(new vTimeframe(new Date(0), new Date()));
  546. }
  547. /**
  548. *
  549. * @return 01092010
  550. */
  551. public static String getDateNumeric() {
  552. String year = getYear();
  553. String month = getMonth();
  554. String day = getDayOfMonth();
  555. String dn = year + month + day;
  556. if(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("item.date.locale")==null)
  557. mpv5.db.objects.User.getCurrentUser().getProperties().changeProperty("item.date.locale", Locale.getDefault().toString());
  558. try {
  559. Locale l = TypeConversion.stringToLocale(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("item.date.locale"));
  560. if (l.equals(Locale.GERMAN) || l.equals(Locale.GERMANY)) {
  561. dn = day + month + year;
  562. }
  563. } catch (Exception e) {
  564. Log.Debug(e);
  565. } finally {
  566. return dn;
  567. }
  568. }
  569. }