Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created November 7, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesselima/92ed3ff6130eca56926296bed9880402 to your computer and use it in GitHub Desktop.
Save jesselima/92ed3ff6130eca56926296bed9880402 to your computer and use it in GitHub Desktop.
package com.example.myapplication.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public final class DateUtils {
/**
* Allows others classes instantiates a empty DateUtils object.
*/
public DateUtils() {
}
/**
* This method when called receives a date as String format in this pattern: yyyy-MM-dd'T'HH:mm:ss
* and return a date as a String object with this pattern: HH:mm LLL dd, yyyy
*
* @param dateString is the date in a string format is this pattern yyyy-MM-dd'T'HH:mm:ss
* @return the date as a string with this new format.
*/
public static String newsSimpleDateFormat(String dateString) {
String dateStringFormated = null;
Date date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
try {
date = dateFormat.parse(dateString);
dateStringFormated = formatDate(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateStringFormated;
}
/**
* This method receives the Date object as input parameter.
*
* @param dateObject is the Date object to be formated.
* @return a String object with the date formated according to the SimpleDateFormat method pattern: HH:mm LLL dd, yyyy.
*/
private static String formatDate(Date dateObject) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm LLL dd, yyyy", Locale.ENGLISH);
return dateFormat.format(dateObject);
}
/**
* This method when called receives a date as String format in this pattern: yyyy-MM-dd'T'HH:mm:ss
* and return a date as a String object with this pattern: LLL dd, yyyy
*
* @param dateString is the date in a string format is this pattern yyyy-MM-dd'T'HH:mm:ss
* @return the date as a string with this new format.
*/
public static String datePickerFormat(String dateString) {
String dateStringFormated = null;
Date date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
date = dateFormat.parse(dateString);
dateStringFormated = selectedDatePattern(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateStringFormated;
}
private static String selectedDatePattern(Date dateObject) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM, yyyy", Locale.ENGLISH);
return dateFormat.format(dateObject);
}
// Return as date as a String "yyyy-DD-dd".
public static String buildMyDate(int year, int month, int day) {
String stringDate = "";
stringDate += (year);
stringDate += ("-");
stringDate += (month);
stringDate += ("-");
stringDate += (day);
return stringDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment