List of usage examples for java.lang Long parseLong
public static long parseLong(String s) throws NumberFormatException
From source file:Main.java
public static String getPackageTime(Context context) { String channelInfo = getChannelInfo(context); if (TextUtils.isEmpty(channelInfo)) { return ""; }//from w ww . j a va 2s. co m String[] infos = channelInfo.split(SPLIT_REGULAR); if (infos.length > 1) { return formatDate(Long.parseLong(channelInfo.split(SPLIT_REGULAR)[1])); } else { return ""; } }
From source file:Main.java
public static long parse(String str, long fallback) { try {/*from w w w. j a va 2 s. co m*/ return Long.parseLong(str); } catch (NumberFormatException e) { return fallback; } }
From source file:Main.java
private static long getLongParamFromUri(Uri uri, String paramName, long defaultValue) { final String value = uri.getQueryParameter(paramName); if (!TextUtils.isEmpty(value)) { try {/*from w ww . j a va 2s . co m*/ return Long.parseLong(value); } catch (NumberFormatException e) { // return default } } return defaultValue; }
From source file:Main.java
public static long readLongAttribute(XmlPullParser in, String name, long defaultValue) { final String value = in.getAttributeValue(null, name); try {/*from www . jav a2 s . c om*/ return Long.parseLong(value); } catch (NumberFormatException e) { return defaultValue; } }
From source file:Main.java
public static long getNodeValue(final Node parentNode, final String strNodeName, final long lDefaultValue) { final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null); if (strValue != null) { return Long.parseLong(strValue); }/*w w w . j av a 2 s . c o m*/ return lDefaultValue; }
From source file:Main.java
/** * Parses the supplied xsd:long string and returns its value. * //from www. j a va 2 s . co m * @param s * A string representation of an xsd:long value. * @return The <tt>long</tt> value represented by the supplied string argument. * @throws NumberFormatException * If the supplied string is not a valid xsd:long value. */ public static long parseLong(String s) { s = trimPlusSign(s); return Long.parseLong(s); }
From source file:Main.java
public static long getTimeAsMillisecs(final Object iSize) { if (iSize == null) throw new IllegalArgumentException("Time is null"); if (iSize instanceof Number) // MILLISECS return ((Number) iSize).longValue(); String time = iSize.toString(); boolean number = true; for (int i = time.length() - 1; i >= 0; --i) { if (!Character.isDigit(time.charAt(i))) { number = false;// w ww . ja v a2 s. c o m break; } } if (number) // MILLISECS return Long.parseLong(time); else { time = time.toUpperCase(Locale.ENGLISH); int pos = time.indexOf("MS"); if (pos > -1) return Long.parseLong(time.substring(0, pos)); pos = time.indexOf("S"); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * SECOND; pos = time.indexOf("M"); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * MINUTE; pos = time.indexOf("H"); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * HOUR; pos = time.indexOf("D"); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * DAY; pos = time.indexOf('W'); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * WEEK; pos = time.indexOf('Y'); if (pos > -1) return Long.parseLong(time.substring(0, pos)) * YEAR; // RE-THROW THE EXCEPTION throw new IllegalArgumentException("Time '" + time + "' has a unrecognizable format"); } }
From source file:Main.java
public static Collection<Long> toIdCollection(String idList) { if (TextUtils.isEmpty(idList)) return new ArrayList<Long>(); String[] idArray = idList.split(","); Collection<Long> ids = new ArrayList<Long>(idArray.length); for (String id : idArray) { ids.add(Long.parseLong(id)); }/*w w w . jav a 2 s. com*/ return ids; }
From source file:ctlogger.CTlogger.java
public static void main(String args[]) { /**//from ww w . j a v a 2s . c o m * * Original code for command line parsing * (This has been replaced by code using Apache Commons CLI, see below) * String helpMsg = "CTlogger -x -r -z -g -k <skiplines> -f <flush_sec> -p <poll_sec> -n <nanVal> -i <leadingID> -s <SourceName> -H <HeaderLine> <logger.dat> <CTfolder>"; int dirArg = 0; while((dirArg<args.length) && args[dirArg].startsWith("-")) { // arg parsing if(args[dirArg].equals("-h")) { System.err.println(helpMsg); System.exit(0); } if(args[dirArg].equals("-x")) { debug = true; } if(args[dirArg].equals("-b")) { noBackwards = true; } if(args[dirArg].equals("-g")) { gzipmode = true; } // default false if(args[dirArg].equals("-a")) { appendMode = false; } // default true if(args[dirArg].equals("-z")) { zipmode = false; } // default true if(args[dirArg].equals("-N")) { newFileMode = true; } // default false if(args[dirArg].equals("-f")) { autoflush = Long.parseLong(args[++dirArg]); } if(args[dirArg].equals("-p")) { pollInterval = Long.parseLong(args[++dirArg]); } if(args[dirArg].equals("-k")) { skipLines = Long.parseLong(args[++dirArg]); } if(args[dirArg].equals("-r")) { repeatFetch = true; } if(args[dirArg].equals("-B")) { blockMode = true; } if(args[dirArg].equals("-t")) { storeTime = true; } if(args[dirArg].equals("-T")) { trimTime = Double.parseDouble(args[++dirArg]); } if(args[dirArg].equals("-n")) { nanVal = args[++dirArg]; } if(args[dirArg].equals("-i")) { leadingID = args[++dirArg]; } if(args[dirArg].equals("-s")) { SourceName = args[++dirArg]; } if(args[dirArg].equals("-H")) { HeaderLine = args[++dirArg]; } dirArg++; } if(args.length < (dirArg+2)) { System.err.println(helpMsg); System.exit(0); } loggerFileName = args[dirArg++]; // args[0]: logger.dat file CTrootfolder = args[dirArg++]; // args[1]: CT destination folder */ // // Parse command line arguments // // 1. Setup command line options // Options options = new Options(); // Boolean options (only the flag, no argument) options.addOption("h", "help", false, "Print this message"); options.addOption("x", "debug", false, "turn on debug output"); options.addOption("b", "nobackwards", false, "no backwards-going time allowed"); options.addOption("g", "gzipmode", false, "turn on gzip for extra compression"); options.addOption("a", "noappend", false, "turn off append mode (i.e., do not append to end of existing CT data)"); options.addOption("z", "nozip", false, "turn off zip mode (it is on by default)"); options.addOption("N", "newfilemode", false, "re-parse entire logger file every time it is checked"); options.addOption("r", "repeatFetch", false, "turn on repeat fetch (auto-fetch data loop)"); options.addOption("B", "blockMode", false, "turn on CloudTurbine writer block mode (multiple points per output data file, packed data)"); options.addOption("t", "storeTime", false, "store time string as a channel; time is the first data entry in each line; if this option is not specified, then the time channel is skipped/not saved to CloudTurbine"); // Options with an argument Option outputFolderOption = Option.builder("f").argName("autoflush").hasArg() .desc("flush interval (sec); default = \"" + autoflush + "\"").build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("p").argName("pollInterval").hasArg().desc( "if repeatFetch option has been specified, recheck the logger data file at this polling interval (sec); default = \"" + pollInterval + "\"") .build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("k").argName("skipLines").hasArg().desc( "in logger file, the num lines to skip after the header line to get to the first line of data; default = \"" + skipLines + "\"") .build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("T").argName("trimTime").hasArg().desc( "trim (ring-buffer loop) time (sec) (trimTime=0 for indefinite); default = \"" + trimTime + "\"") .build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("n").argName("nanVal").hasArg() .desc("replace NAN with this; default = \"" + nanVal + "\"").build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("i").argName("leadingID").hasArg() .desc("leading ID string (IWG1 compliant)").build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("s").argName("sourceName").hasArg() .desc("CloudTurbine source name; default = \"" + SourceName + "\"").build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("H").argName("HeaderLine").hasArg().desc( "optional CSV list of channel names; if not supplied, this is read from the first line in the logger file") .build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("l").argName("loggerfilename").hasArg() .desc("name of the logger data file; required argument").build(); options.addOption(outputFolderOption); outputFolderOption = Option.builder("o").longOpt("outputfolder").argName("folder").hasArg() .desc("Location of output files (source is created under this folder); default = " + CTrootfolder) .build(); options.addOption(outputFolderOption); // // 2. Parse command line options // CommandLineParser parser = new DefaultParser(); CommandLine line = null; try { line = parser.parse(options, args); } catch (org.apache.commons.cli.ParseException exp) { // oops, something went wrong System.err.println("Command line argument parsing failed: " + exp.getMessage()); return; } // // 3. Retrieve the command line values // if (line.hasOption("help")) { // Display help message and quit HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("CTlogger", options); return; } debug = line.hasOption("x"); noBackwards = line.hasOption("b"); gzipmode = line.hasOption("g"); appendMode = !line.hasOption("a"); zipmode = !line.hasOption("z"); newFileMode = line.hasOption("N"); repeatFetch = line.hasOption("r"); blockMode = line.hasOption("B"); storeTime = line.hasOption("t"); autoflush = Long.parseLong(line.getOptionValue("f", Long.toString(autoflush))); pollInterval = Long.parseLong(line.getOptionValue("p", Long.toString(pollInterval))); skipLines = Long.parseLong(line.getOptionValue("k", Long.toString(skipLines))); trimTime = Double.parseDouble(line.getOptionValue("T", Double.toString(trimTime))); nanVal = line.getOptionValue("n", nanVal); if (line.hasOption("i")) { leadingID = line.getOptionValue("i"); } SourceName = line.getOptionValue("s", SourceName); if (line.hasOption("H")) { HeaderLine = line.getOptionValue("H"); } if (line.hasOption("l")) { loggerFileName = line.getOptionValue("l"); } else { System.err.println("ERROR: you must supply the logger file name."); return; } CTrootfolder = line.getOptionValue("o", CTrootfolder); if (!debug) { System.err.println("CTlogger: " + loggerFileName + ", CTrootfolder: " + CTrootfolder + ", pollInterval: " + pollInterval); } else { System.err.println("debug = " + debug); System.err.println("noBackwards = " + noBackwards); System.err.println("gzipmode = " + gzipmode); System.err.println("appendMode = " + appendMode); System.err.println("zipmode = " + zipmode); System.err.println("newFileMode = " + newFileMode); System.err.println("repeatFetch = " + repeatFetch); System.err.println("blockMode = " + blockMode); System.err.println("storeTime = " + storeTime); System.err.println("autoflush = " + autoflush); System.err.println("pollInterval = " + pollInterval); System.err.println("skipLines = " + skipLines); System.err.println("trimTime = " + trimTime); System.err.println("nanVal = " + nanVal); System.err.println("leadingID = " + leadingID); System.err.println("SourceName = " + SourceName); System.err.println("HeaderLine = " + HeaderLine); System.err.println("loggerFileName = " + loggerFileName); System.err.println("CTrootfolder = " + CTrootfolder); } // // Run CTlogger // if (!repeatFetch) getData(true); // run once else { Timer timer = new Timer(); TimerTask fetchTask = new TimerTask() { @Override public void run() { if (newFileMode) getData(true); else if (getData(false)) { // pick up from old data if you can System.err.println("Failed to pick up from old data, refetch from start of file..."); boolean status = getData(true); System.err.println("refetch status: " + status); } if (debug) System.err.println("Waiting for data, pollInterval: " + pollInterval + " sec..."); }; }; // repeatFetch@autoflush interval, convert to msec if ((autoflush > 0) && (pollInterval > autoflush)) pollInterval = autoflush; timer.scheduleAtFixedRate(fetchTask, 0, pollInterval * 1000); } }
From source file:Main.java
public static String durationToFloat(String duration) { String[] parts = duration.split("\\:"); if (parts.length == 2) { long minute = Long.parseLong(parts[0]); long seconds = Long.parseLong(parts[1]); if (seconds == 60) { minute = minute + 1;/* w w w. j a v a 2 s . c o m*/ seconds = 0; } else { seconds = (100 * seconds) / 60; } return String.format("%d.%d", minute, seconds); } return "false"; }