Search in sources :

Example 91 with ParsePosition

use of java.text.ParsePosition in project h2o-2 by h2oai.

the class ASTNum method parseNumberWithScientificNotationProperlyHandled.

/**
   * Parse a scientific number more correctly for commands passed in from R.
   * Unfortunately, NumberFormat.parse doesn't get the job done.
   * It expects 'E' and can't handle 'e' or 'E+nnn'.
   *
   * @param s String to parse
   * @param startPosition Starting position in the string to parse from.
   * @param charactersConsumed [output] Characters consumed.
   * @return The parsed value if one was found, null otherwise.  If a value was parsed, charactersConsumed will be set to something greater than 0.  If no value was parsed, charactersConsumed will be 0.
   */
private static double parseNumberWithScientificNotationProperlyHandled(String s, final int startPosition, MyInteger charactersConsumed) {
    // Paranoid.
    charactersConsumed._val = 0;
    ParsePosition pp = new ParsePosition(startPosition);
    Number N = NF.parse(s, pp);
    if (// If no number was found, just return immediately.
    pp.getIndex() == startPosition)
        return 0;
    assert N instanceof Double || N instanceof Long;
    // Check if the number we just parsed had an 'e' or 'E' in it.  So it's scientific already.
    for (int i = startPosition; i < pp.getIndex(); i++) {
        char c = s.charAt(i);
        if ((c == 'e') || (c == 'E')) {
            // We already got a scientific number.  Return it.
            charactersConsumed._val = pp.getIndex() - startPosition;
            if (N instanceof Double)
                return (Double) N;
            return (double) (Long) N;
        }
    }
    // If we consumed all of str, then just return the value now.
    assert (pp.getIndex() <= s.length());
    if (pp.getIndex() >= s.length()) {
        charactersConsumed._val = pp.getIndex() - startPosition;
        if (N instanceof Double)
            return (Double) N;
        return (double) (Long) N;
    }
    // If the lookahead character is not 'e' then just return the value now.
    char lookaheadChar = s.charAt(pp.getIndex());
    if ((lookaheadChar != 'e') && (lookaheadChar != 'E')) {
        charactersConsumed._val = pp.getIndex() - startPosition;
        if (N instanceof Double)
            return (Double) N;
        return (double) (Long) N;
    }
    // The lookahead character is 'e'.  Find the remaining trailing numbers
    // and attach them to this token.
    // Start with sb as stuff from NF.parse plus the 'e'.
    StringBuilder sb = new StringBuilder();
    sb.append(s.substring(startPosition, Math.min(s.length(), pp.getIndex() + 2)));
    for (int i = pp.getIndex() + 2; i < s.length(); i++) {
        char c = s.charAt(i);
        if (// Only +-digits allowed after that.
        c != '+' && c != '-' && !Character.isDigit(c))
            break;
        sb.append(c);
    }
    // consider it a number.
    try {
        double d = Double.valueOf(sb.toString());
        // Set length consumed before return
        charactersConsumed._val = sb.length();
        return d;
    }// No set length; just return.
     catch (Exception e) {
        return 0;
    }
}
Also used : ParsePosition(java.text.ParsePosition)

Example 92 with ParsePosition

use of java.text.ParsePosition in project translationstudio8 by heartsome.

the class DateUtils method getNowDate.

/**
	 * 获取现在时间
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
public static Date getNowDate() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString = formatter.format(currentTime);
    ParsePosition pos = new ParsePosition(8);
    Date currentTime_2 = formatter.parse(dateString, pos);
    return currentTime_2;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 93 with ParsePosition

use of java.text.ParsePosition in project translationstudio8 by heartsome.

the class DateUtils method getEDate.

/**
	 * 返回美国时间格式 26 Apr 2006
	 * @param str
	 * @return
	 */
public static String getEDate(String str) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(str, pos);
    String j = strtodate.toString();
    String[] k = j.split(" ");
    return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 94 with ParsePosition

use of java.text.ParsePosition in project translationstudio8 by heartsome.

the class DateUtils method getTimestampFromUTC.

public static java.sql.Timestamp getTimestampFromUTC(String utcFormat) {
    if (utcFormat == null || utcFormat.equals("")) {
        return null;
    }
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(utcFormat, pos);
    if (strtodate == null) {
        strtodate = new Date();
    }
    return new java.sql.Timestamp(strtodate.getTime());
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 95 with ParsePosition

use of java.text.ParsePosition in project translationstudio8 by heartsome.

the class DateUtils method formatDateFromUTC.

public static String formatDateFromUTC(String utcFormat) {
    if (utcFormat == null || utcFormat.equals("")) {
        return null;
    }
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(utcFormat, pos);
    if (strtodate == null) {
        strtodate = new Date();
    }
    return formatLongTime(strtodate.getTime(), "yyyy-MM-dd,HH:mm");
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Aggregations

ParsePosition (java.text.ParsePosition)694 Test (org.junit.Test)250 Date (java.util.Date)213 TemporalAccessor (java.time.temporal.TemporalAccessor)163 SimpleDateFormat (java.text.SimpleDateFormat)117 DateTimeFormatter (java.time.format.DateTimeFormatter)94 Test (org.testng.annotations.Test)88 ParseException (java.text.ParseException)71 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)49 SimpleDateFormat (android.icu.text.SimpleDateFormat)39 FieldPosition (java.text.FieldPosition)32 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)25 Calendar (android.icu.util.Calendar)23 ULocale (android.icu.util.ULocale)23 NumberFormat (java.text.NumberFormat)23 Calendar (java.util.Calendar)23 Format (java.text.Format)21 DecimalFormat (java.text.DecimalFormat)19 GregorianCalendar (android.icu.util.GregorianCalendar)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)17