Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateUtils {
//Assigning default DateFormat
private String mDateFormat = JODADateFormats.DATE_PATTERN_SERVICE;
private long mDateAsMillis;
//Returning DateTimeFormatter form default DateFormat value
private DateTimeFormatter mDateTimeFormatter = DateTimeFormat
.forPattern(mDateFormat);
//By default current date is picked instead of returning null date
private String mDateAsString = getCurrentDateInString();
public static DateUtils with (){
return new DateUtils();
}
public DateUtils date(String date){
this.mDateAsString = date;
return this;
}
public DateUtils date(long timestamp){
this.mDateAsMillis = timestamp;
return this;
}
public DateUtils dateFormat(String dateFormat) {
DateTimeFormatter outPutFormat = DateTimeFormat
.forPattern(dateFormat);
this.mDateFormat = dateFormat;
this.mDateTimeFormatter = outPutFormat;
return this;
}
public String getDateAsIST() {
DateTimeFormatter utcFormatter = mDateTimeFormatter.withLocale(Locale.US)
.withZoneUTC();
DateTimeZone indianTimeZone = DateTimeZone.forID(JODATimeZone.INDIA);
DateTimeFormatter indianZoneFormatter = utcFormatter
.withZone(indianTimeZone);
DateTime parsed = utcFormatter.parseDateTime(mDateAsString);
String indianText = indianZoneFormatter.print (parsed);
return indianText;
}
public String getDateAsUTC() {
DateTimeZone indianTimeZone = DateTimeZone.forID (JODATimeZone.INDIA);
DateTimeFormatter indianZoneFormatter = mDateTimeFormatter.withLocale(Locale.US)
.withZone (indianTimeZone);
DateTimeFormatter utcZoneFormatter = indianZoneFormatter
.withZone(DateTimeZone.UTC);
DateTime parsed = indianZoneFormatter.parseDateTime(mDateAsString);
String indianText = utcZoneFormatter.print (parsed);
return indianText;
}
private HashMap<String, String> getDateInNameFormat(String dateTime){
HashMap<String, String> hMap = new HashMap<String,String>();
DateTime jodatime = mDateTimeFormatter.parseDateTime(
dateTime);
hMap.put(JODAKey.MONTH, jodatime.toString("MMM"));
hMap.put(JODAKey.DAY_NAME, jodatime.toString("EEE"));
hMap.put(JODAKey.DAY_NUMBER, jodatime.toString("dd"));
hMap.put(JODAKey.YEAR, jodatime.toString("yyyy"));
hMap.put (JODAKey.TIME_IN_24_HOURS_FOMAT, jodatime.toString ("HH:mm"));
hMap.put (JODAKey.TIME_IN_12_HOURS_FOMAT, jodatime.toString ("hh:mm"));
hMap.put (JODAKey.HOURS_24_FORMAT, jodatime.toString ("HH"));
hMap.put(JODAKey.HOURS_12_FORMAT, jodatime.toString ("hh"));
hMap.put (JODAKey.MINUTES, jodatime.toString ("mm"));
hMap.put (JODAKey.AM_OR_PM, jodatime.toString ("a"));
return hMap;
}
public String getDateFromKey(String key){
return getDateInNameFormat (mDateAsString).get (key);
}
public String getDateFormatAfterOneHour() {
DateTime jodatime = mDateTimeFormatter.parseDateTime (mDateAsString); // Printing the date
return (mDateTimeFormatter.print(jodatime.plusHours (1)));
}
public String getDateFormatAfterOneDay() {
DateTime jodatime = mDateTimeFormatter.parseDateTime (mDateAsString); // Printing the date
return (mDateTimeFormatter.print(jodatime.plusDays (1)));
}
public Date getDateInDateFormat() {
DateTime jodatime = mDateTimeFormatter.parseDateTime(
mDateAsString);
return jodatime.toDate ();
}
public String getDateAsStringFromDate(Date date){
DateTime dateTime = new DateTime(date);
return mDateTimeFormatter.print (dateTime);
}
public long getTotalYearsDifferenceFromDate (String toDate){
DateTime fromDateTime = mDateTimeFormatter.parseDateTime(
mDateAsString);
DateTime toDateTime = mDateTimeFormatter.parseDateTime(
toDate);
Period period = new Period (fromDateTime,toDateTime);
return period.getYears ();
}
public long getTotalDayssDifferenceFromDate (String toDate){
DateTime fromDateTime = mDateTimeFormatter.parseDateTime(
mDateAsString);
DateTime toDateTime = mDateTimeFormatter.parseDateTime(
toDate);
Duration duration = new Duration (fromDateTime,toDateTime);
return duration.getStandardDays ();
}
public long getTotalHoursDifferenceFromDate (String toDate){
DateTime fromDateTime = mDateTimeFormatter.parseDateTime(
mDateAsString);
DateTime toDateTime = mDateTimeFormatter.parseDateTime(
toDate);
Duration duration = new Duration (fromDateTime,toDateTime);
return duration.getStandardHours ();
}
public long getTotalMinutesDifferenceFromDate (String toDate){
DateTime fromDateTime = mDateTimeFormatter.parseDateTime(
mDateAsString);
DateTime toDateTime = mDateTimeFormatter.parseDateTime(
toDate);
Duration duration = new Duration (fromDateTime,toDateTime);
return duration.getStandardMinutes ();
}
public long getTotalSecondsDifferenceFromDate (String toDate){
DateTime fromDateTime = mDateTimeFormatter.parseDateTime(
mDateAsString);
DateTime toDateTime = mDateTimeFormatter.parseDateTime(
toDate);
Duration duration = new Duration (fromDateTime,toDateTime);
return duration.getStandardSeconds ();
}
public long getHoursDifferenceIn24hFormat(String toDate) {
DateTime actualDateTime = mDateTimeFormatter
.parseDateTime(mDateAsString);
DateTime choosenDateTime = mDateTimeFormatter
.parseDateTime (toDate);
Period period = new Period(actualDateTime, choosenDateTime);
long hours = (period.getHours());
return hours;
}
public long getMinutesDifferenceIn24hFormat(String toDate) {
DateTime actualDateTime = mDateTimeFormatter
.parseDateTime(mDateAsString);
DateTime choosenDateTime = mDateTimeFormatter
.parseDateTime (toDate);
Period period = new Period(actualDateTime, choosenDateTime);
long minutesConversion = getHoursDifferenceIn24hFormat(toDate) * 60;
long minutes = minutesConversion+(period.getMinutes());
return minutes;
}
public long getSecondsDifferenceIn24hFormat(String toDate) {
DateTime actualDateTime = mDateTimeFormatter
.parseDateTime(mDateAsString);
DateTime choosenDateTime = mDateTimeFormatter
.parseDateTime (toDate);
Period period = new Period(actualDateTime, choosenDateTime);
long secondsConversion = getMinutesDifferenceIn24hFormat(toDate) * 60;
long seconds = secondsConversion+(period.getSeconds());
return seconds;
}
public String getDateAsStringFromMillis(long timestamp) {
DateTime dt = new DateTime(timestamp);
String dateString = dt.toString(mDateFormat);
return dateString;
}
public long getDateAsMillis () {
DateTime jodatime = mDateTimeFormatter.parseDateTime (mDateAsString);
return jodatime.getMillis();
}
public long asMillis (String fromDate) {
DateTime jodatime = mDateTimeFormatter.parseDateTime (fromDate);
return jodatime.getMillis();
}
public Calendar getDateAsGregorianCalendar(){
DateTimeFormatter formatter =
DateTimeFormat.forPattern(mDateFormat).withOffsetParsed();
DateTime dateTime = formatter.parseDateTime(mDateAsString);
GregorianCalendar cal = dateTime.toGregorianCalendar ();
return cal;
}
public long getCurrentDateInMillis() {
DateTime date = new DateTime();
return date.getMillis();
}
public String getCurrentDateInString() {
DateTime dateTime = new DateTime();
return dateTime.toString(mDateTimeFormatter);
}
public String buildDateFromParameters(String year, String month, String day, String hours, String minutes, String seconds){
return year+"-"+month+"-"+day+"-"+" "+hours+":"+minutes+":"+seconds;
}
}
public class JODADateFormats {
public static final String DATE_PATTERN_SERVICE = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_PATTERN_SERVICE_NO_TIME = "yyyy-MM-dd";
public static final String DATE_PATTERN_SERVICE_NO_SECONDS = "yyyy-MM-dd HH:mm";
public static final String DATE_PATTERN_IN_NAME = "dd/EEE/MMM/yyyy/HH:mm";
}
public class JODAKey {
public static final String MONTH = "month_name";
public static final String DAY_NAME = "day_name";
public static final String DAY_NUMBER = "day";
public static final String YEAR = "year";
public static final String TIME_IN_24_HOURS_FOMAT = "time_24_format";
public static final String TIME_IN_12_HOURS_FOMAT = "time_12_format";
public static final String HOURS_24_FORMAT = "hours_24_format";
public static final String HOURS_12_FORMAT = "hours_12_format";
public static final String MINUTES = "minutes";
public static final String AM_OR_PM = "meridiem";
}
public class JODATimeZone {
public static final String INDIA = "Asia/Kolkata";
}
public class MainActivity {
public static void main(String[] args) {
long timestamp = 1446629473263L;
String input = "1991-12-06 01:05:00";
// String dateDiff = "2015-12-10 18:17:00";
System.out.println(DateUtils.with().date(input).dateFormat(JODADateFormats.DATE_PATTERN_SERVICE).getDateFromKey(JODAKey.DAY_NAME));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.