Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package itomych.com.jti.util;
- import android.text.format.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Locale;
- /**
- *
- * DateUtils class.
- *
- * TODO 18-05-2018 (alexischenko): most of this class code is crap, please do not use it
- * Note
- *
- * Most of DateUtils methods served ProfileEdit screen needs which at the current moment (2018)are not included to screen classes ()
- */
- public class DateUtils {
- /* T
- sd
- */
- public static final String DATE_FORMAT_PATTERN = "dd.MM.yyyy";
- public static final String YYYY_MM_DD_T_HH_MM_SS_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
- public static final String DD_MM_YYYY = "dd/MM/yyyy";
- private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
- /**
- * used to display user b-day date on UI
- */
- private static final String SHORT_DATE_FORMAT_PATTERN = "dd/MM/yyyy";
- public static String getCurrentDateFormatted() {
- Date c = Calendar.getInstance().getTime();
- SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- return df.format(c);
- }
- /**
- * Method used by current class getShortDate() only
- *
- */
- private static Date getDate(String date) {
- try {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_PATTERN, Locale.getDefault());
- simpleDateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
- return simpleDateFormat.parse(date);
- } catch (ParseException e) {
- return new Date();
- }
- }
- /**
- * Method used by current class getShortDate() only
- *
- */
- private static String format(Date date, String pattern) {
- try {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
- simpleDateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
- return simpleDateFormat.format(date);
- } catch (Exception e) {
- return "";
- }
- }
- /**
- * Used by UserProfileFragment
- * TODO: 18-05-2018: move this method to User (jti.data.model) class
- */
- public static String getShortDate(String date) {
- return format(getDate(date), SHORT_DATE_FORMAT_PATTERN);
- }
- public static String getDateSubstractYears(int yearsToAdd) {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- Date date = sdf.parse(getCurrentTime());
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.YEAR, yearsToAdd);
- return sdf.format(calendar.getTime());
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
- public static String getCurrentTime() {
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- return dateFormat.format(calendar.getTime());
- }
- public static long getTimestamp(String date) {
- try {
- Date localDate = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.getDefault()).parse(date);
- return localDate.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public static int getYear(String formattedDate) {
- SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- try {
- Date date = format.parse(formattedDate);
- return Integer.parseInt((String) DateFormat.format("yyyy", date));
- } catch (ParseException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public static int getMonth(String formattedDate) {
- SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- try {
- Date date = format.parse(formattedDate);
- return Integer.parseInt((String) DateFormat.format("MM", date));
- } catch (ParseException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public static int getDayOfMonth(String formattedDate) {
- SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- try {
- Date date = format.parse(formattedDate);
- return Integer.parseInt((String) DateFormat.format("dd", date));
- } catch (ParseException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public static String getFormattedDate(int year, int month, int day) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(year, month, day, 0, 0);
- SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
- return dateFormat.format(calendar.getTime());
- }
- }
Add Comment
Please, Sign In to add comment