Skip to content

Instantly share code, notes, and snippets.

@dassiorleando
Last active January 20, 2020 07:25
Show Gist options
  • Save dassiorleando/f68c7bc77a357090a0d1 to your computer and use it in GitHub Desktop.
Save dassiorleando/f68c7bc77a357090a0d1 to your computer and use it in GitHub Desktop.
A JodaTime and java.util.Date Util Class with a lot of usefull functions.
package cm.flashbusiness.core.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
/**
* @author dassi
*/
public class DateTimeUtil {
public static final String FOMAT_ONE = "dd/MM/yyyy HH:mm";
public static final Integer CURRENT_YEAR = DateTime.now().getYear();
public static final Integer CURRENT_MONTH = DateTime.now().getMonthOfYear();
public static final Integer CURRENT_DAY = DateTime.now().getDayOfMonth();
public static final Integer LAST_MONTH_OF_CURRENT_YEAR = DateTime.now()
.monthOfYear().getMaximumValue();
public static final Integer FIRST_MONTH_OF_CURRENT_YEAR = DateTime.now()
.monthOfYear().getMinimumValue();
public static final Integer LAST_DAY_OF_CURRENT_MONTH = DateTime.now()
.dayOfMonth().getMaximumValue();
public static final Integer FIRST_DAY_OF_CURRENT_MONTH = DateTime.now()
.dayOfMonth().getMinimumValue();
public static final Integer LAST_HOUR_OF_CURRENT_DAY = DateTime.now()
.hourOfDay().getMaximumValue();
public static final Integer FIRST_HOUR_OF_CURRENT_DAY = DateTime.now()
.hourOfDay().getMinimumValue();
public static final Integer LAST_MINUTE_OF_CURRENT_HOUR = DateTime.now()
.minuteOfHour().getMaximumValue();
public static final Integer FIRST_MINUTE_OF_CURRENT_HOUR = DateTime.now()
.minuteOfHour().getMinimumValue();
public static final Integer LAST_SECOND_OF_CURRENT_MINUTE = DateTime.now()
.secondOfMinute().getMaximumValue();
public static final Integer FIRST_SECOND_OF_CURRENT_MINUTE = DateTime.now()
.secondOfMinute().getMinimumValue();
public static DateTime getFirstDateOfDay() {
return new DateTime(CURRENT_YEAR, CURRENT_MONTH, CURRENT_DAY,
FIRST_HOUR_OF_CURRENT_DAY, FIRST_MINUTE_OF_CURRENT_HOUR,
FIRST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getLastDateOfDay() {
return new DateTime(CURRENT_YEAR, CURRENT_MONTH, CURRENT_DAY,
LAST_HOUR_OF_CURRENT_DAY, LAST_MINUTE_OF_CURRENT_HOUR,
LAST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getFirstDateOfMonth() {
return new DateTime(CURRENT_YEAR, CURRENT_MONTH,
FIRST_DAY_OF_CURRENT_MONTH, FIRST_HOUR_OF_CURRENT_DAY,
FIRST_MINUTE_OF_CURRENT_HOUR, FIRST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getLastDateOfMonth() {
return new DateTime(CURRENT_YEAR, CURRENT_MONTH,
LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY,
LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getFirstDateOfYear() {
return new DateTime(CURRENT_YEAR, FIRST_MONTH_OF_CURRENT_YEAR,
FIRST_DAY_OF_CURRENT_MONTH, FIRST_HOUR_OF_CURRENT_DAY,
FIRST_MINUTE_OF_CURRENT_HOUR, FIRST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getLastDateOfYear() {
return new DateTime(CURRENT_YEAR, LAST_MONTH_OF_CURRENT_YEAR,
LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY,
LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE);
}
public static DateTime getLastDateOfDate(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(),
dateTime.getDayOfMonth(), dateTime.hourOfDay()
.getMaximumValue(), dateTime.minuteOfHour()
.getMaximumValue(), dateTime.secondOfMinute()
.getMaximumValue());
}
public static DateTime getFirstDateOfDate(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(),
dateTime.getDayOfMonth(), dateTime.hourOfDay()
.getMinimumValue(), dateTime.minuteOfHour()
.getMinimumValue(), dateTime.secondOfMinute()
.getMinimumValue());
}
public static DateTime getFirstDateOfMonth(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(),
dateTime.dayOfMonth().getMinimumValue(), dateTime.hourOfDay()
.getMinimumValue(), dateTime.minuteOfHour()
.getMinimumValue(), dateTime.secondOfMinute()
.getMinimumValue());
}
public static DateTime getLastDateOfMonth(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(),
dateTime.dayOfMonth().getMaximumValue(), dateTime.hourOfDay()
.getMaximumValue(), dateTime.minuteOfHour()
.getMaximumValue(), dateTime.secondOfMinute()
.getMaximumValue());
}
public static DateTime getLastDateOfYear(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.monthOfYear()
.getMaximumValue(), dateTime.dayOfMonth().getMaximumValue(),
dateTime.hourOfDay().getMaximumValue(), dateTime.minuteOfHour()
.getMaximumValue(), dateTime.secondOfMinute()
.getMaximumValue());
}
public static DateTime getFirstDateOfYear(DateTime dateTime) {
return new DateTime(dateTime.getYear(), dateTime.monthOfYear()
.getMinimumValue(), dateTime.dayOfMonth().getMinimumValue(),
dateTime.hourOfDay().getMinimumValue(), dateTime.minuteOfHour()
.getMinimumValue(), dateTime.secondOfMinute()
.getMinimumValue());
}
public static String converterToString(Date date, String format){
if(date == null){
return "";
}
SimpleDateFormat formater = new SimpleDateFormat(format);
return formater.format(date);
}
public static Date converterToDate(String stringDate) throws ParseException{
SimpleDateFormat formater = new SimpleDateFormat(FOMAT_ONE);
return formater.parse(stringDate);
}
}
@ToanPV90
Copy link

Very usefull.thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment