Package com.appspot.finajjarane.framework.generic

Source Code of com.appspot.finajjarane.framework.generic.Utils

package com.appspot.finajjarane.framework.generic;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.springframework.context.i18n.LocaleContextHolder;

import com.appspot.finajjarane.framework.models.DateDetailsModel;

public class Utils {

  public static String getLanguage(){
    return LocaleContextHolder.getLocale().getLanguage() + "_" + LocaleContextHolder.getLocale().getCountry();
  }

  public static DateDetailsModel dateToDateDetailsModel(final Date date, String lang){

    if(null==lang){
      lang = Utils.getLanguage();
    }

    DateDetailsModel dateModel = new DateDetailsModel();
    Calendar calendar = Calendar.getInstance();

    dateModel.setDate(date);
    calendar.setTime(date);

    String dateFormat = Utils.getTranslatedMessage("date.format", lang);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);


    if(null!=date){
      dateModel.setDayNumber(calendar.get(Calendar.DAY_OF_WEEK));
      dateModel.setMonthNumber(calendar.get(Calendar.MONTH));
      dateModel.setYearNumber(calendar.get(Calendar.YEAR));
      dateModel.setHour(calendar.get(Calendar.HOUR_OF_DAY));
      dateModel.setMinute(calendar.get(Calendar.MINUTE));
      dateModel.setSeconde(calendar.get(Calendar.SECOND));
      dateModel.setFormattedDate(simpleDateFormat.format(date));
    }


    return dateModel;
  }

  public static DateDetailsModel dateToDateDetailsModel(final Date date){
    return dateToDateDetailsModel(date, null);
  }

  public static String getTranslatedMessage(String key, String lang){
    Locale locale;

    if(null==lang){
      locale = LocaleContextHolder.getLocale();
    }
    else{
      locale = new Locale(lang);
    }
    return ResourceBundle.getBundle("messages",locale).getString(key);
  }


  public static long getDatesDifferenceInSenconds(Date referenceDate, Date currentDate, boolean abs){

    Calendar calendarForCurrent = Calendar.getInstance();
    Calendar calendarForReference = Calendar.getInstance();

    calendarForCurrent.setTime(currentDate);
    calendarForReference.setTime(referenceDate);

    if(abs){
      return Math.abs(calendarForReference.getTimeInMillis() - calendarForCurrent.getTimeInMillis())/1000;
    }
    else{
      return (calendarForReference.getTimeInMillis() - calendarForCurrent.getTimeInMillis())/1000;
    }

  }


  public static String slugify(String str){
    return str.replaceAll("~[^\\\\pL\\d]+~u", "-").trim().replaceAll("~[^-\\w]+~", "");
  }
}
TOP

Related Classes of com.appspot.finajjarane.framework.generic.Utils

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.