Package net.javalib.util.i18n

Source Code of net.javalib.util.i18n.Lang

package net.javalib.util.i18n;

import java.text.*;
import java.text.DateFormat;
import java.util.*;

/**
* @author Sherif Omar
*
* Lang.java
*
*/
public class Lang {
   
    protected static String getDate(Date date, int pattern, boolean allowNull) {
        if (date == null && !allowNull)
            return new String();
        if (date == null)
            date = getCalendar().getTime();
        return getDateFormat(pattern).format(date);
    }
   
    protected static String getTime(Date date, int pattern, boolean allowNull) {
        if (date == null && !allowNull)
            return new String();
        if (date == null)
            date = getCalendar().getTime();
        return getTimeFormat(pattern).format(date);
    }   
   
    protected static String getDateTime(Date date, int pattern, boolean allowNull) {
        if (date == null && !allowNull)
            return new String();  
        if (date == null)
            getCalendar().getTime();     
        return getDateTimeFormat(pattern, pattern).format(date);
    }   
   
    public static Date currentDateTime() {
        return getCalendar().getTime();
    }
   
    public static String fullDateTime(Date date) {
        return getDateTime(date, DateFormat.FULL, true);
    }   
   
    public static String longDateTime(Date date) {
        return getDateTime(date, DateFormat.LONG, true);
    }
   
    public static String mediumDateTime(Date date) {
        return getDateTime(date, DateFormat.MEDIUM, true);
    }
   
    public static String shortDateTime(Date date) {
        return getDateTime(date, DateFormat.SHORT, true);
    }    
   
    public static String fullDate(Date date, boolean allowNull) {
        return getDate(date, DateFormat.FULL, allowNull);
    }   
   
    public static String fullDate(Date date) {
        return longDate(date, false);
    }
   
    public static String fullDate() {
        return fullDate(null, true);
    }  
   
    public static String longDate(Date date, boolean allowNull) {
        return getDate(date, DateFormat.LONG, allowNull);
    }   
   
    public static String longDate(Date date) {
        return longDate(date, false);    
    }
   
    public static String longDate() {
        return longDate(null, true);
    }   
   
    public static String mediumDate(Date date, boolean allowNull) {
        return getDate(date, DateFormat.MEDIUM, allowNull);
    }   
   
    public static String mediumDate(Date date) {
        return mediumDate(date, false);       
    }
   
    public static String mediumDate() {
        return mediumDate(null, true);
    }
   
    public static String shortDate(Date date, boolean allowNull) {
        return getDate(date, DateFormat.SHORT, allowNull);
    }   
   
    public static String shortDate(Date date) {
        return shortDate(date, false);
    }
   
    public static String shortDate() {
        return shortDate(null, true);
    }   
   
    public static String shortTime(Date date, boolean allowNull) {
        return getTime(date, DateFormat.SHORT, allowNull);
    }     
   
    public static String shortTime(Date date) {
        return shortTime(date, false);
    }     
   
    public static String shortTime() {
        return shortTime(null, true);
    }    
   
    public static int getDay(Date date) {
      Calendar cal = getCalendar();
      cal.setTime(date);
      return cal.get(Calendar.DAY_OF_MONTH);
    }   
   
    public static String getDayName(Date date) {
      Calendar cal = getCalendar();
      cal.setTime(date);
      return getDayNames()[cal.get(Calendar.DAY_OF_WEEK)];
    }
   
    public static String getDatePattern() {
        SimpleDateFormat dformat = (SimpleDateFormat)getDateFormat(DateFormat.SHORT);
        return dformat.toPattern();
    }
   
    public static DateFormatSymbols getDateFormatSymbols() {
        SimpleDateFormat dformat = (SimpleDateFormat)getDateFormat();
        return dformat.getDateFormatSymbols();     
    }
   
    public static String[] getMonthNames() {
        return getDateFormatSymbols().getMonths();
    }
   
    public static String[] getShortMonthNames() {
        return getDateFormatSymbols().getShortMonths();
    }   
   
    public static String[] getDayNames() {
        return getDateFormatSymbols().getWeekdays();
    }   

    public static String[] getShortDayNames() {
        return getDateFormatSymbols().getShortWeekdays();
    }     
   
    //
    // always use the functions below so they create instances according to proper user locale
    //
   
    public static Calendar getCalendar() {
        return Calendar.getInstance(TimeZone.getDefault());
    }
   
    public static DateFormat getDateFormat() {
        return DateFormat.getDateInstance();
    }
   
    public static DateFormat getTimeFormat() {
        return DateFormat.getTimeInstance();
    }   
   
    public static DateFormat getDateFormat(int pattern) {
        return DateFormat.getDateInstance(pattern);
    }   
   
    public static DateFormat getTimeFormat(int pattern) {
        return DateFormat.getTimeInstance(pattern);
    }       
   
    public static DateFormat getShortDateFormat() {
        DateFormat df = getDateFormat(DateFormat.SHORT);
        df.setLenient(false);
        return df;
    }
   
    public static DateFormat getShortDateTimeFormat() {
        DateFormat df = getDateTimeFormat(DateFormat.SHORT, DateFormat.SHORT);
        df.setLenient(false);
        return df;
    }   
   
    public static DateFormat getDateTimeFormat(int dateStyle, int timeStyle) {
        return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
    }      
   
    public static NumberFormat getNumberFormat() {
        return NumberFormat.getInstance();
    }
   
    public static String formatNumber(Number number) {
        return getNumberFormat().format(number);
    }
   
    // ---------------------------------------------------------------------------------------
   
    public static Date makeTimeInsensitiveMin(Date date) {
     
      if (date == null) return null;     
       
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
       
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));       
       
        return cal.getTime();
    }   
   
    public static Date makeTimeInsensitiveMax(Date date) {
     
      if (date == null) return null;
       
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
       
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));       
       
        return cal.getTime();
    }      
   
    // ---------------------------------------------------------------------------------------   
   
    public static int currentMonth() {
      return getCalendar().get(Calendar.MONTH);
    }   
   
    public static int currentYear() {
      return getCalendar().get(Calendar.YEAR);
    }
   
    public static boolean isToday(Date date) {
      Calendar dateCalendar = getCalendar();
      dateCalendar.setTime(date);
      Calendar nowCalendar = getCalendar();
     
      return (dateCalendar.get(Calendar.DATE) == nowCalendar.get(Calendar.DATE)
          && dateCalendar.get(Calendar.MONTH) == nowCalendar.get(Calendar.MONTH)
        && dateCalendar.get(Calendar.YEAR) == nowCalendar.get(Calendar.YEAR));
    }
   
    // ---------------------------------------------------------------------------------------   
   
}
TOP

Related Classes of net.javalib.util.i18n.Lang

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.