Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. package com.gitlab.danxes.utility;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.Locale;
  9.  
  10. import lombok.extern.slf4j.Slf4j;
  11.  
  12. /**
  13. * Utility class for date and time formatting with locale support.
  14. *
  15. * @author muhammad
  16. */
  17. @Slf4j
  18. public class LocaleDateUtils {
  19.  
  20. private LocaleDateUtils() {}
  21.  
  22. /**
  23. * The default JVM locale.
  24. */
  25. private static Locale locale = Locale.getDefault();
  26.  
  27. public static Locale getLocale() {
  28. return locale;
  29. }
  30.  
  31. public static void setLocale(Locale locale) {
  32. LocaleDateUtils.locale = locale;
  33. }
  34.  
  35. public static final long MILLIS_PER_SECOND = 1000;
  36. public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
  37. public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
  38. public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
  39.  
  40. private static final int[] PATTERN_STYLES = new int[] {DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL};
  41.  
  42. public static String[] datePatterns(Locale locale) {
  43. String[] datePatterns = new String[PATTERN_STYLES.length];
  44. int i = 0;
  45. for (int df : PATTERN_STYLES) {
  46. SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(df, locale);
  47. datePatterns[i] = sdf.toLocalizedPattern();
  48. i++;
  49. }
  50. return datePatterns;
  51. }
  52.  
  53. public static String[] timePatterns(Locale locale) {
  54. String[] timePatterns = new String[PATTERN_STYLES.length];
  55. int i = 0;
  56. for (int tf : PATTERN_STYLES) {
  57. SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getTimeInstance(tf, locale);
  58. timePatterns[i] = sdf.toLocalizedPattern();
  59. i++;
  60. }
  61. return timePatterns;
  62. }
  63.  
  64. public static String[] dateTimePatterns(Locale locale) {
  65. String[] dateTimePatterns = new String[PATTERN_STYLES.length * PATTERN_STYLES.length];
  66. int i = 0;
  67. for (int df : PATTERN_STYLES) {
  68. for (int tf : PATTERN_STYLES) {
  69. SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance(df, tf, locale);
  70. dateTimePatterns[i] = sdf.toLocalizedPattern();
  71. i++;
  72. }
  73. }
  74. return dateTimePatterns;
  75. }
  76.  
  77. public static String[] allPatterns(Locale locale) {
  78. int datePatternsCount = PATTERN_STYLES.length;
  79. int timePatternsCount = PATTERN_STYLES.length;
  80. int dateTimePatternsCount = datePatternsCount * timePatternsCount;
  81. String[] allPatterns = new String[datePatternsCount + timePatternsCount + dateTimePatternsCount];
  82. int dfCount = 0;
  83. int loop = 0;
  84. for (int df : PATTERN_STYLES) {
  85. SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(df, locale);
  86. String datePattern = sdf.toLocalizedPattern();
  87. allPatterns[loop] = datePattern;
  88. loop++;
  89. for (int tf : PATTERN_STYLES) {
  90. sdf = (SimpleDateFormat) DateFormat.getTimeInstance(tf, locale);
  91. String timePattern = sdf.toLocalizedPattern();
  92. String dateTimePattern = datePattern + " " + timePattern;
  93. allPatterns[loop] = dateTimePattern;
  94. loop++;
  95. if (dfCount == 0) {
  96. allPatterns[loop] = timePattern;
  97. loop++;
  98. }
  99. }
  100. dfCount++;
  101. }
  102. return allPatterns;
  103. }
  104.  
  105. public static String[] datePatterns() {
  106. return datePatterns(locale);
  107. }
  108.  
  109. public static String[] timePatterns() {
  110. return timePatterns(locale);
  111. }
  112.  
  113. public static String[] dateTimePatterns() {
  114. return dateTimePatterns(locale);
  115. }
  116.  
  117. public static String[] allPatterns() {
  118. return allPatterns(locale);
  119. }
  120.  
  121. /**
  122. * Returns the number of days between 2 dates.
  123. *
  124. * @param olderDate the older date to count the days from
  125. * @param newerDate the newer date to count the days until
  126. * @return The number of days between 2 dates.
  127. */
  128. public static long daysBetween(Date olderDate, Date newerDate) {
  129. Calendar cal = Calendar.getInstance();
  130. cal.setTime(olderDate);
  131. cal.set(Calendar.HOUR_OF_DAY, 0);
  132. cal.set(Calendar.MINUTE, 0);
  133. cal.set(Calendar.SECOND, 0);
  134. cal.set(Calendar.MILLISECOND, 0);
  135.  
  136. Date dateFrom = cal.getTime();
  137.  
  138. cal.setTime(newerDate);
  139. cal.set(Calendar.HOUR_OF_DAY, 0);
  140. cal.set(Calendar.MINUTE, 0);
  141. cal.set(Calendar.SECOND, 0);
  142. cal.set(Calendar.MILLISECOND, 0);
  143.  
  144. Date dateTo = cal.getTime();
  145.  
  146. return Math.round((dateTo.getTime() - dateFrom.getTime()) / ((double) MILLIS_PER_DAY));
  147. }
  148.  
  149. /**
  150. * Returns new date object that has been added with specified amount for specified
  151. * date field.
  152. *
  153. * @param date the date.
  154. * @param field the calendar field.
  155. * @param amount the amount of date or time to be added to the field.
  156. * @return a date after being added
  157. */
  158. public static Date add(Date date, int field, int amount) {
  159. Calendar cal = Calendar.getInstance();
  160. cal.setTime(date);
  161. cal.add(field, amount);
  162. return cal.getTime();
  163. }
  164.  
  165. public static Date substract(Date date, int field, int amount) {
  166. return add(date, field, -amount);
  167. }
  168.  
  169. /**
  170. * Parses a string to {@link Date} object using specified array of date
  171. * patterns. First pattern that matches the date format of the given string
  172. * will be used for parsing.
  173. *
  174. * @param dateString string to parse to Date
  175. * @param datePatterns array of date patterns to use for parsing
  176. * @param locale locale to use for parsing
  177. * @return A {@link Date} object of specified string
  178. */
  179. public static Date toDate(String dateString, String[] datePatterns, Locale locale) {
  180. Date date = null;
  181. for (String pattern : datePatterns) {
  182. try {
  183. SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
  184. date = sdf.parse(dateString);
  185. } catch (ParseException pe) {
  186. log.warn("Cannot parse date with pattern: {}", pattern);
  187. }
  188. if (null != date) {
  189. break;
  190. }
  191. }
  192. return date;
  193. }
  194.  
  195. /**
  196. * Parses a string to {@link Date} object using default array of date
  197. * patterns. First pattern that matches the date format of the given string
  198. * will be used for parsing.
  199. *
  200. * @param dateString string to parse to Date
  201. * @param locale locale to use for parsing
  202. * @return A {@link Date} object of specified string
  203. */
  204. public static Date toDate(String dateString, Locale locale) {
  205. return toDate(dateString, allPatterns(locale), locale);
  206. }
  207.  
  208. /**
  209. * Parses a string to {@link Date} object using default array of date patterns
  210. * and default locale. First pattern that matches the date format of
  211. * the given string will be used for parsing.
  212. *
  213. * @param dateString string to parse to Date
  214. * @return A {@link Date} object of specified string
  215. */
  216. public static Date toDate(String dateString) {
  217. return toDate(dateString, locale);
  218. }
  219.  
  220. /**
  221. * Parses a string to {@link Date} object using specified array of date
  222. * patterns and default locale. First pattern that matches the date format
  223. * of the given string will be used for parsing.
  224. *
  225. * @param dateString string to parse to Date
  226. * @param datePatterns array of date patterns to use for parsing
  227. * @return A {@link Date} object of specified string
  228. */
  229. public static Date toDate(String dateString, String[] datePatterns) {
  230. return toDate(dateString, datePatterns, locale);
  231. }
  232.  
  233. /**
  234. * Parses a string to {@link Date} object using specified date pattern and locale.
  235. *
  236. * @param dateString string to parse to Date
  237. * @param datePattern date pattern to use for parsing
  238. * @param locale locale to use for parsing
  239. * @return A {@link Date} object of specified string
  240. */
  241. public static Date toDate(String dateString, String datePattern, Locale locale) {
  242. return toDate(dateString, new String[] {datePattern}, locale);
  243. }
  244.  
  245. /**
  246. * Parses a string to {@link Date} object using specified date pattern and default locale.
  247. *
  248. * @param dateString string to parse to Date
  249. * @param datePattern date pattern to use for parsing
  250. * @return A {@link Date} object of specified string
  251. */
  252. public static Date toDate(String dateString, String datePattern) {
  253. return toDate(dateString, datePattern, locale);
  254. }
  255.  
  256. /**
  257. * Returns formatted date string of given date millisecond.
  258. *
  259. * @param milliseconds date millisecond since Jan 1, 1970
  260. * @param pattern date pattern for formatting
  261. * @param locale locale to use for formatting
  262. * @return Formatted date string
  263. */
  264. public static String toString(long milliseconds, String pattern, Locale locale) {
  265. String dateString;
  266. SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
  267. dateString = sdf.format(milliseconds);
  268. return dateString;
  269. }
  270.  
  271. /**
  272. * Returns formatted date string of given date object.
  273. *
  274. * @param date date object to be formatted
  275. * @param pattern date pattern for formatting
  276. * @param locale locale to use for formatting
  277. * @return Formatted date string
  278. */
  279. public static String toString(Date date, String pattern, Locale locale) {
  280. return toString(date.getTime(), pattern, locale);
  281. }
  282.  
  283. /**
  284. * Returns formatted date string of given date object.
  285. *
  286. * @param date date object to be formatted
  287. * @param pattern date pattern for formatting
  288. * @return Formatted date string
  289. */
  290. public static String toString(Date date, String pattern) {
  291. return toString(date, pattern, locale);
  292. }
  293.  
  294. /**
  295. * Returns formatted date string of given date millisecond.
  296. *
  297. * @param milliseconds date millisecond since Jan 1, 1970
  298. * @param pattern date pattern for formatting
  299. * @return Formatted date string
  300. */
  301. public static String toString(long milliseconds, String pattern) {
  302. return toString(milliseconds, pattern, locale);
  303. }
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement