Example usage for java.util Locale getDefault

List of usage examples for java.util Locale getDefault

Introduction

In this page you can find the example usage for java.util Locale getDefault.

Prototype

public static Locale getDefault() 

Source Link

Document

Gets the current value of the default locale for this instance of the Java Virtual Machine.

Usage

From source file:Main.java

public static String convertToHumanReadableTime(Date givenDate, long currentTimeLong) {

    Calendar currentTime = Calendar.getInstance();
    currentTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    currentTime.setTimeInMillis(currentTimeLong);

    Calendar givenTime = Calendar.getInstance();
    givenTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    givenTime.setTime(givenDate);//from w  ww.j  av  a 2 s .  c om

    // Step 1: To see if time difference is less than 24 hours of not
    long timeDiff = currentTime.getTimeInMillis() - givenTime.getTimeInMillis();
    if (timeDiff <= 0) {
        return "Now";
    }

    String humanString = null;
    // Checking if timeDiff is less than 24 or not
    if ((timeDiff / TIME_DAY) >= 1) {
        // days
        int days = (int) (timeDiff / TIME_DAY);
        humanString = String.format(Locale.getDefault(), "%dd", days);
    } else {
        // checking if greater than hour
        if ((timeDiff / TIME_HOUR) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dh", (timeDiff / TIME_HOUR));
        } else if ((timeDiff / TIME_MINUTE) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dm", (timeDiff / TIME_MINUTE));
        } else {
            humanString = String.format(Locale.getDefault(), "%ds", (timeDiff / TIME_MILLIS));
        }
    }

    return humanString;
}

From source file:Main.java

/**
 * This method determines if the direction of a substring is right-to-left.
 * If the string is empty that determination is based on the default system language
 * Locale.getDefault().//w  w w  .  j  av a  2 s  . c  o  m
 * The method can handle invalid substring definitions (start > end etc.), in which case the
 * method returns False.
 *
 * @return True if the text direction is right-to-left, false otherwise.
 */
public static boolean isRTL(CharSequence s, int start, int end) {
    if (s == null || s.length() == 0) {
        // empty string --> determine the direction from the default language
        return isRTL(Locale.getDefault());
    }

    if (start == end) {
        // if no character is selected we need to expand the selection
        start = Math.max(0, --start);
        if (start == end) {
            end = Math.min(s.length(), ++end);
        }
    }

    try {
        Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        return !bidi.baseIsLeftToRight();
    } catch (IndexOutOfBoundsException e) {
        return false;
    }
}

From source file:Main.java

public static String getMD5(String value) {
    if (value == null || value.length() == 0)
        return null;

    MessageDigest m = null;/*  ww  w .  j  av  a  2s .  c om*/

    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    m.update(value.getBytes(), 0, value.length());
    return new BigInteger(1, m.digest()).toString(16).toUpperCase(Locale.getDefault());
}

From source file:Main.java

public static long string2Millis(String time, String pattern) {
    try {// w w  w  .ja va 2s .co m
        return new SimpleDateFormat(pattern, Locale.getDefault()).parse(time).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

/**
 * Generate a User-Agent used in HTTP request to pick an ad.
 * Source used from Android source code "frameworks/base/core/java/android/webkit/WebSettings.java"
 * /*from  w  w w .  ja  v a  2 s .  c o  m*/
 * @return
 */
protected static String getUA() {
    if (UA != null)
        return UA;

    StringBuffer arg = new StringBuffer();

    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        arg.append(version);
    } else {
        arg.append("1.0");
    }
    arg.append("; ");

    final Locale l = Locale.getDefault();
    final String language = l.getLanguage();
    if (language != null) {
        arg.append(language.toLowerCase());
        final String country = l.getCountry();
        if (country != null) {
            arg.append("-");
            arg.append(country.toLowerCase());
        }
    } else {
        arg.append("de");
    }
    final String model = Build.MODEL;
    if (model.length() > 0) {
        arg.append("; ");
        arg.append(model);
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        arg.append(" Build/");
        arg.append(id);
    }

    // TODO: add version detection for AppleWebKit, Version and Safari
    final String rawUA = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
    UA = String.format(rawUA, arg);

    return UA;
}

From source file:Main.java

/**
 * Generate a User-Agent used in HTTP request to pick an ad. Source used
 * from Android source code/*from   w ww .j  av a  2s  .com*/
 * "frameworks/base/core/java/android/webkit/WebSettings.java"
 * 
 * @return
 */
public static String getUA() {
    if (sUA != null)
        return sUA;

    StringBuffer arg = new StringBuffer();

    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        arg.append(version);
    } else {
        arg.append("1.0");
    }
    arg.append("; ");

    final Locale l = Locale.getDefault();
    final String language = l.getLanguage();
    if (language != null) {
        arg.append(language.toLowerCase());
        final String country = l.getCountry();
        if (country != null) {
            arg.append("-");
            arg.append(country.toLowerCase());
        }
    } else {
        arg.append("de");
    }
    final String model = Build.MODEL;
    if (model.length() > 0) {
        arg.append("; ");
        arg.append(model);
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        arg.append(" Build/");
        arg.append(id);
    }

    // TODO: add version detection for AppleWebKit, Version and Safari
    final String rawUA = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
    sUA = String.format(rawUA, arg);

    return sUA;
}

From source file:Main.java

private static void insertClientToWedding(SQLiteDatabase db, String nameClient, boolean weddingType,
        long weddingDate, String weddingPlace, String groomFullName, String groomPhone, String groomSoc,
        String brideFullName, String bridePhone, String brideSoc, int weddingBegin, int weddingEnd,
        String weddingMeet, int visaj, int visArr, int barber, int barberArr, int videoOp, int vidArr,
        int groomArr, String zagsName, int zagsFillTime, String walkDesc, String walkPlace, String banketPlace,
        int banketBegin, String wit1, String wit1Soc, String wit2, String wit2Soc, int photoAuthorCount,
        String photoFormat, String photoBook, int photoBookSize, int pbookTurns, int weddingCost, int garant,
        long dateOfPact, String weddingNote) {
    ContentValues cv = new ContentValues();
    cv.put("NAME", nameClient);
    cv.put("WEDDING_TYPE", weddingType);
    cv.put("WEDDING_DATE", weddingDate);
    cv.put("WEDDING_DATE_STRING",
            new SimpleDateFormat("dd MMM yy E", Locale.getDefault()).format(new Date(weddingDate)));
    cv.put("WEDDING_PLACE", weddingPlace);
    cv.put("WEDDING_GROOM", groomFullName);
    cv.put("WEDDING_GROOM_PHONE", groomPhone);
    cv.put("WEDDING_GROOM_SOC", groomSoc);
    cv.put("WEDDING_BRIDE", brideFullName);
    cv.put("WEDDING_BRIDE_PHONE", bridePhone);
    cv.put("WEDDING_BRIDE_SOC", brideSoc);
    cv.put("WEDDING_BEGIN", weddingBegin);
    cv.put("WEDDING_END", weddingEnd);
    cv.put("WEDDING_MEET", weddingMeet);
    cv.put("WEDDING_VIS", visaj);
    cv.put("WEDDING_VIS_ARR", visArr);
    cv.put("WEDDING_BARBER", barber);
    cv.put("WEDDING_BARBER_ARR", barberArr);
    cv.put("WEDDING_CAMOP", videoOp);
    cv.put("WEDDING_CAMOP_ARR", vidArr);
    cv.put("WEDDING_GROOM_ARR", groomArr);
    cv.put("WEDDING_ZAGS", zagsName);
    cv.put("WEDDING_ZAGS_FILLTIME", zagsFillTime);
    cv.put("WEDDING_WALK", walkDesc);
    cv.put("WEDDING_WALK_PLACE", walkPlace);
    cv.put("WEDDING_BANKET_PLACE", banketPlace);
    cv.put("WEDDING_BANKET_START", banketBegin);
    cv.put("WEDDING_WIT1", wit1);
    cv.put("WEDDING_WIT1_SOC", wit1Soc);
    cv.put("WEDDING_WIT2", wit2);
    cv.put("WEDDING_WIT2_SOC", wit2Soc);
    cv.put("WEDDING_PHOT_AUT", photoAuthorCount);
    cv.put("WEDDING_PHOT_FORMAT", photoFormat);
    cv.put("WEDDING_PHOT_BOOK", photoBook);
    cv.put("WEDDING_PBOOK_SIZE", photoBookSize);
    cv.put("WEDDING_PBOOK_TURNS", pbookTurns);
    cv.put("WEDDING_COST", weddingCost);
    cv.put("WEDDING_GARANT", garant);
    cv.put("WEDDING_PACT_DATE", dateOfPact);
    cv.put("WEDDING_NOTE", weddingNote);
    db.insert("WEDDING", null, cv);

}

From source file:Main.java

/**
 * Call this method on the Activity onResume. It will recreate the Activity if a Locale change is detected.
 * @param activity/*from   w w w  .  ja  va  2  s.  c  o  m*/
 */
public static void onResume(Activity activity) {
    Locale previousLocale = localesOnActivities.get(activity.toString());
    boolean shouldRestartActivity = previousLocale != null && !previousLocale.equals(Locale.getDefault());

    localesOnActivities.put(activity.toString(), Locale.getDefault());

    if (shouldRestartActivity) {
        recreate(activity, false);
    }
}

From source file:Main.java

/***
 * Fetch the estimate travel time to the indicated target
 * @param distance - how far to the target
 * @param speed - how fast we are moving
 * @param calculated - already calculated value
 * @param calc - or should I calculate?/*w  w  w.j  av  a  2 s.com*/
 * @return String - "HH:MM" or "MM.SS" time to the target
 */
public static String calculateEte(double distance, double speed, long calculated, boolean calc) {

    // If no speed, then return the empty display value
    if (0 == speed && calc) {
        return "--:--";
    }

    // Fetch the eteRaw value
    Time eteRaw = fetchRawEte(distance, speed, calculated, calc);

    // If an invalid eteRaw, then return the empty display value
    if (null == eteRaw) {
        return "--:--";
    }

    // Break the eteRaw out into hours and minutes
    int eteHr = eteRaw.hour;
    int eteMin = eteRaw.minute;
    int eteSecond = eteRaw.second;

    // Hours greater than 99 are not displayable
    if (eteHr > 99) {
        return "XX:XX";
    }

    // If hours is non zero then return HH:MM
    if (eteHr > 0) {
        // Format the hours and minutes en router
        String hr = String.format(Locale.getDefault(), "%02d", eteHr);
        String min = String.format(Locale.getDefault(), "%02d", eteMin);

        // Build the string for return
        return hr + ":" + min;
    }

    // Hours is zero, so return MM.SS
    String min = String.format(Locale.getDefault(), "%02d", eteMin);
    String sec = String.format(Locale.getDefault(), "%02d", eteSecond);

    // Build the string for return
    return min + "." + sec;

}

From source file:Main.java

static String localize(Locale locale, String baseName, String messageId, Object... parameters) {
    //Log.v(LOG_TAG, String.format("locale=%s, baseName=%s, messageId=%s, parameters=%s", locale, baseName, messageId, Arrays.asList(parameters)));
    if (locale == null) {
        locale = Locale.getDefault();
    }/*  w ww .ja va2  s.c om*/
    try {
        ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale);
        String source = bundle.getString(messageId);
        return MessageFormat.format(source, parameters);
    } catch (MissingResourceException e) {
        return "message '" + messageId + "' could not be localized (" + e.getMessage() + ")";
    }
}