nosi.core.webapp.helpers.DateHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of igrp-core Show documentation
Show all versions of igrp-core Show documentation
IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.
package nosi.core.webapp.helpers;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import nosi.core.webapp.Core;
import java.sql.Timestamp;
/**
* @author: Emanuel Pereira
* 14 Aug 2017
*/
public class DateHelper {
private static final int CALCULATE_AGE = 1;
private static final int CALCULATE_MONTHS = 2;
private static final int CALCULATE_DAYS = 3;
public static String convertDate(String date, String formatIn, String outputFormat) {
String myDateString = null;
if(Core.isNotNull(date) && Core.isNotNull(outputFormat) && Core.isNotNull(formatIn)) {
try {
SimpleDateFormat newDateFormat = new SimpleDateFormat(formatIn);
Date myDate = newDateFormat.parse(date);
newDateFormat.applyPattern(outputFormat);
myDateString = newDateFormat.format(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return myDateString;
}
public static java.sql.Date formatDate(String data,String inputFormat){
if(Core.isNotNull(data) && Core.isNotNull(inputFormat)) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(inputFormat);
return new java.sql.Date(formatter.parse(data).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
public static java.sql.Date formatDate(String data,String inputFormat,String outputFormat){
if(Core.isNotNull(data) && Core.isNotNull(outputFormat) && Core.isNotNull(inputFormat)) {
DateFormat formatter = new SimpleDateFormat(inputFormat);
Date date;
try {
date = formatter.parse(data);
SimpleDateFormat newFormat = new SimpleDateFormat(outputFormat);
String finalDate = newFormat.format(date);
return java.sql.Date.valueOf(finalDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
public static java.sql.Date getCurrentDate(){
return new java.sql.Date(System.currentTimeMillis());
}
public static java.util.Date getCurrentDateUtil(){
return new java.util.Date(System.currentTimeMillis());
}
public static java.util.Calendar getCurrentDateCalendar(){
return java.util.Calendar.getInstance();
}
public static String getCurrentDate(String outputFormat){
return convertDate(getCurrentDate().toString(), "yyyy-MM-dd", outputFormat);
}
public static String getCurrentDataTime() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
return dateFormat.format(new Date());
}
public static String convertDateToString(java.sql.Date date,String outputFormat) {
DateFormat dateFormat = new SimpleDateFormat(outputFormat);
return dateFormat.format(date);
}
public static java.sql.Date convertStringToDate(String date,String outputFormat) {
return formatDate(date,outputFormat);
}
public static String convertTimeStampToDateString(String timeStampDate,String outputFormat) {
if(Core.isNotNull(timeStampDate)) {
return convertDate(timeStampDate, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", outputFormat);
}
return null;
}
public static java.sql.Date convertTimeStampToDate(String timeStampDate,String outputFormat) {
if(Core.isNotNull(timeStampDate)) {
return formatDate(timeStampDate, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", outputFormat);
}
return null;
}
public static Timestamp convertStringToTimestamp(String str_date,String format) {
try {
DateFormat formatter;
formatter = new SimpleDateFormat(format);
Date date = formatter.parse(str_date);
return new Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static java.sql.Date utilDateToSqlDate(java.util.Date date) {
return new java.sql.Date(date.getTime());
}
private static long calculate(String data,String formatIn,int type) {
long age = 0;
try {
data = Core.convertDate(data, formatIn,"yyyy-MM-dd");
LocalDate birthDate = LocalDate.parse(data);
LocalDate currentDate = LocalDate.now();
return switch (type) {
case CALCULATE_AGE -> ChronoUnit.YEARS.between(birthDate, currentDate);
case CALCULATE_DAYS -> ChronoUnit.DAYS.between(birthDate, currentDate);
case CALCULATE_MONTHS ->
ChronoUnit.MONTHS.between(birthDate.withDayOfMonth(1), currentDate.withDayOfMonth(1));
default -> age;
};
} catch (Exception e) {
e.printStackTrace();
}
return age;
}
public static long calculateYears(String data) {
return calculate(data, "dd-MM-yyyy",CALCULATE_AGE);
}
public static long calculateYears(String data,String formatIn) {
return calculate(data, formatIn,CALCULATE_AGE);
}
public static long calculateMonths(String data) {
return calculate(data, "dd-MM-yyyy",CALCULATE_MONTHS);
}
public static long calculateMonths(String data,String formatIn) {
return calculate(data, formatIn,CALCULATE_MONTHS);
}
public static long calculateDays(String data) {
return calculate(data, "dd-MM-yyyy",CALCULATE_DAYS);
}
public static long calculateDays(String data,String formatIn) {
return calculate(data, formatIn,CALCULATE_DAYS);
}
}