substring
public String substring(int beginIndex,int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符。因此,该子字符串的长度为endIndex-beginIndex。
示例:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
参数: beginIndex- 起始索引(包括). endIndex- 结束索引(不包括)。
返回:指定的子字符串。
抛出: indexOfBoundsException- 如果beginIndex为负,或endIndex大于此String对象的长度,或beginIndex大于endIndex。
compareTo
public int compareTo(String anotherString)
按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此String对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此String对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此String对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;compareTo只在方法equals(Objects)返回true时才返回0。
这是字典排序的定义。如果这两个字符串不同,那么它们要么在某个索引处的字符不同(该索引对二者均为有效索引),要么长度不同,或者同时具备这两种情况。如果它们在一个或多个索引位置上的字符不同,假设 k 是这类索引的最小值;则在位置 k 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo返回这两个字符串在位置k处两个char 值的差,即值: this.charAt(k)-anotherString.charAt(k)
如果没有字符不同的索引位置,则较短字符串的字典顺序在较长字符串之前。在这种情况下,compareTo
返回这两个字符串长度的差,即值: this.length()-anotherString.length()
指定者:接口 Comparable<String>
中的 compareTo
参数:anotherString
- 要比较的 String
。
返回:
如果参数字符串等于此字符串,则返回值0;如果此字符串按字典顺序小于字符串参数,则返回一个小于0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于0 的值。
public List<OrderInfo> getOrderList(List<String> dateList, List<order_info> list,int queryType) { List<OrderInfo> orderList = new ArrayList<OrderInfo>(); if (dateList != null && dateList.size() > 0) { for (String date : dateList) { OrderInfo order = new OrderInfo(); order.setDate(date); order.setOrder_count(0); order.setSale_count(BigDecimal.ZERO); if (list != null && list.size() > 0) { for (order_info orderInfo : list) { if (queryType == 5) { String beginDate = date.split("~")[0];//正则表达式 //另一种 String beginDate=date.subString(0,10); String endDate = date.split("~")[1]; if (orderInfo.get("date").toString().compareTo(beginDate) >= 0 && orderInfo.get("date").toString().compareTo(endDate) <= 0) { if (orderInfo.get("order_count") != null) { order.setOrder_count(order.getOrder_count() + orderInfo.<Long>get("order_count")); } if (orderInfo.get("sale_count") != null) { double saleCount = order.getSale_count().doubleValue() + orderInfo.<BigDecimal>get("sale_count").doubleValue(); order.setSale_count(BigDecimal.valueOf(saleCount)); } } } else { if (date.equals(orderInfo.get("date"))) { if (orderInfo.get("order_count") != null) { order.setOrder_count(orderInfo.<Long>get("order_count")); } if (orderInfo.get("sale_count") != null) { order.setSale_count(orderInfo.<BigDecimal>get("sale_count")); } } } } } orderList.add(order); } } return orderList; }