Created
October 14, 2019 16:15
-
-
Save davidsanguinetti/fa511380786b194f68235ec4d56c9724 to your computer and use it in GitHub Desktop.
Util casse, to manage Dates, and misc stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Month/Year auxiliar class | |
* Recomendation: 0 - January -- Just like the Calendar class | |
*/ | |
public class MonthYear { | |
private int month, | |
year; | |
public MonthYear(int month, int year) { | |
this.month = month; | |
this.year = year; | |
} | |
public int getMonth() { | |
return month; | |
} | |
public void setMonth(int month) { | |
this.month = month; | |
} | |
public int getYear() { | |
return year; | |
} | |
public void setYear(int year) { | |
this.year = year; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import model.MonthYear; | |
/** | |
* Created by dsangui on 12/31/17. | |
*/ | |
public class Util { | |
public static final String DEFAULT_TIMEFORMAT = "yyyy-MM-dd'T'HH:mm:ss"; | |
public static final String DEFAULT_TIMEFORMAT_DASH = "MM/dd/yy h:mm:ss a"; | |
public static final String DEFAULT_TIMEFORMAT_SIMPLE = "MM/dd/yyyy"; | |
public static final String DEFAULT_TIMEFORMAT_SIMPLE2 = "MM/dd/yy"; | |
public static final String TIMEFORMAT_COMPLETE = "EEEE, dd MMMM y"; | |
public static final String TIMEFORMAT_TIME = "h:mm a"; | |
public static final String TIMEFORMAT_DATE = "MMM d, yyyy"; | |
public static Date getDate(String datetime) { | |
try { | |
return new SimpleDateFormat(DEFAULT_TIMEFORMAT).parse(datetime); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static Date getDate(String datetime, String dateFormat) { | |
try { | |
return new SimpleDateFormat(dateFormat).parse(datetime); | |
} catch (ParseException e) { | |
// e.printStackTrace(); | |
return Calendar.getInstance().getTime(); | |
} | |
// return null; | |
} | |
public static String getDateFormated(String format, Date date) { | |
if (date == null) | |
return new SimpleDateFormat(format).format(Calendar.getInstance()); | |
else | |
return new SimpleDateFormat(format).format(date); | |
} | |
public static String filterText(String text) { | |
if (text != null) { | |
if (text.isEmpty()) | |
return "-"; | |
else if (text.compareTo("NA") == 0) | |
return "-"; | |
else | |
return text; | |
} | |
else | |
return "-"; | |
} | |
public static Calendar getCalendar(Date dt) { | |
Calendar c = Calendar.getInstance(); | |
c.setTime(dt); | |
return c; | |
} | |
public static String separateUppercaseSpace(String word) { | |
String newWord = new Character(word.charAt(0)).toString().toUpperCase(); | |
for (int i=1 ; i < word.length(); i++) { | |
Character c = word.charAt(i); | |
if (c.toString().toLowerCase().compareTo(c.toString()) == 0) { | |
newWord += c; | |
} else | |
newWord += " " + c; | |
} | |
return newWord; | |
} | |
public static String twoDigits(int number) { | |
return (number < 10) ? "0"+number : String.valueOf(number); | |
} | |
public static MonthYear addMonth(MonthYear my, int nMonths) { | |
if (my == null) | |
return getPresentMonthYear(); | |
Calendar cl = Calendar.getInstance(); | |
cl.set(Calendar.MONTH, my.getMonth()); | |
cl.add(Calendar.MONTH, nMonths); | |
return new MonthYear(cl.get(Calendar.MONTH), cl.get(Calendar.YEAR)); | |
} | |
public static MonthYear previousMonth(MonthYear my, int nMonths) { | |
Calendar cl = Calendar.getInstance(); | |
cl.set(Calendar.MONTH, my.getMonth()); | |
cl.add(Calendar.MONTH, nMonths * (-1)); | |
return new MonthYear(cl.get(Calendar.MONTH), cl.get(Calendar.YEAR)); | |
} | |
public static MonthYear getPresentMonthYear() { | |
Calendar cl = Calendar.getInstance(); | |
MonthYear my = new MonthYear(cl.get(Calendar.MONTH), cl.get(Calendar.YEAR)); | |
return my; | |
} | |
public static String showMonthYear(MonthYear my) { | |
Calendar cl = Calendar.getInstance(); | |
cl.set(Calendar.MONTH, my.getMonth()); | |
cl.set(Calendar.YEAR, my.getYear()); | |
SimpleDateFormat sdf = new SimpleDateFormat("MMMM, yyyy"); | |
return sdf.format(cl.getTime()); | |
} | |
public static String correctTime(String origFormat) { | |
if (origFormat.toLowerCase().contains("am") || origFormat.toLowerCase().contains("pm")) { | |
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm"); | |
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a"); | |
Date date = null; | |
try { | |
date = parseFormat.parse(origFormat); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return displayFormat.format(date); | |
} else | |
return origFormat; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment