Created
March 22, 2017 07:14
-
-
Save ashif-ismail/30f470c5eb9b52219f3d38f18d9a078a to your computer and use it in GitHub Desktop.
Java Utility class to handle various task's related to Date and Time ex:Time concersion and various others
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Ashif on 22/03/17. | |
*/ | |
public class TimeUtils { | |
public static boolean isValidString(String string) { | |
return string != null && !string.isEmpty(); | |
} | |
public static String getStartDate() { | |
Calendar calendar = Calendar.getInstance(); | |
int dayofWeek = calendar.get(Calendar.DAY_OF_WEEK); | |
calendar.setFirstDayOfWeek(Calendar.MONDAY); | |
if (dayofWeek != 7) { | |
int previousWeek = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek(); | |
calendar.add(Calendar.DATE, -previousWeek - 2); | |
} | |
calendar.setFirstDayOfWeek(Calendar.SATURDAY); | |
String startDate= CommonUtils.getDateFormat().format(calendar.getTime()); | |
return startDate; | |
} | |
public static String getCurrentDate() { | |
Date date = new Date(); | |
return getDateFormat().format(date.getTime()); | |
} | |
public static SimpleDateFormat getDateFormat() { | |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); | |
} | |
public static String getCurrentTime(boolean is24HrFormat) { | |
Date date = new Date(); | |
return getTimeFormat(is24HrFormat).format(date.getTime()); | |
} | |
public static SimpleDateFormat getTimeFormat(boolean is24HrFormat) { | |
return new SimpleDateFormat(is24HrFormat ? "HH:mm" : "hh:mm a", Locale.getDefault()); | |
} | |
public static String getCurrentDateTime() { | |
Date date = new Date(); | |
return getDateTimeFormat().format(date.getTime()); | |
} | |
public static SimpleDateFormat getDateTimeFormat() { | |
return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()); | |
} | |
public static SimpleDateFormat getUpdatedDateTimeFormat(){ | |
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault()); | |
} | |
public static Pair<String, String> getDateTimePair(String dateTime) { | |
try { | |
Date parsedDate = getDateTimeFormat().parse(dateTime); | |
String date = getDateFormat().format(parsedDate); | |
String time = getTimeFormat(true).format(parsedDate); | |
return Pair.create(date, time); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return Pair.create(getCurrentDate(), getCurrentTime(true)); | |
} | |
public static String getUserTime(String time) { | |
try { | |
Date parsedTime = getTimeFormat(true).parse(time); | |
return getTimeFormat(false).format(parsedTime); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return getCurrentTime(false); | |
} | |
public static String getCurrentUserDate() { | |
Date date = new Date(); | |
return getUserDateFormat().format(date.getTime()); | |
} | |
public static String getUserDate(String date) { | |
try { | |
Date parsedDate = getDateFormat().parse(date); | |
return getUserDateFormat().format(parsedDate); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return getCurrentUserDate(); | |
} | |
public static SimpleDateFormat getUserDateFormat() { | |
return new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); | |
} | |
public static String convertToJSON(Object object) { | |
ObjectMapper mapper = new ObjectMapper(); | |
try { | |
return mapper.writeValueAsString(object); | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment