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;
}
}
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;
}
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);
}
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());
}
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");
}
Aggregations