我有一个数组,其时间范围如下,
String[] str ={"6.30 AM","6.10 AM","10.00 PM","7.00 PM"};
我希望获得上述数组中的最短时间和最长时间,例如" 6.10 AM"和" 10.00 PM" .i可以找到使用排序但是需要很长时间。有任何其他方法可用。指导我,下面我排序了,
String[] str ={"1:0 PM","2:0 AM","3:0 PM",.....};
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa", Locale.getDefault());
Date TimeToCompare = null,Time1 = null;
for(int i=0;i<10;i++)
{
TimeToCompare=sdf.parse(str[i]);
for(int j=i+1;j<10;j++)
{
Time1=sdf.parse(str[j]);
if(TimeToCompare.after(Time1))
{
//sorting
}
}
}
答案 0 :(得分:1)
此解决方案使一个通过数组,跟踪最小和最大时间。在O(n)中运行。
double maxTime = 0.0;
double minTime = 0.0;
for(String s : str) {
String[] parts = str.split(" ");
double time = Double.parse(parts[0]);
if (parts[1].equals("PM")) {
time += 12;
}
if (time > maxTime) {
maxTime = time;
}
if (time < minTime) {
minTime = time;
}
}
// convert doubles back into strings and print
答案 1 :(得分:0)
这是从ggreiner @
中挑选的示例解决方案How to sort a list of time strings in Java or Groovy
String[] str ={"6.30 AM","6.10 AM","10.00 PM","7.00 PM"};
List<String> times = Arrays.asList(str); // convert int to list
Collections.sort(times, new MyComparator()); // use a custom comparator
Log.i("Min time is ",""+times.get(0));
Log.i("Max Time is ",""+times.get(times.size()-1));
自定义比较器
class MyComparator implements Comparator<String>
{
private DateFormat primaryFormat = new SimpleDateFormat("h.mm a");
@Override
public int compare(String time1, String time2){
return timeInMillis(time1) - timeInMillis(time2);
}
public int timeInMillis(String time){
return timeInMillis(time, primaryFormat);
}
// in milliseconds
private int timeInMillis(String time, DateFormat format) {
Date date = null ;
try {
date = format.parse(time); //
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (int)date.getTime();
}
}
答案 2 :(得分:0)
// Try this way,hope this will help you to solve your problem.
String[] str =new String[]{"6.30 AM","6.10 AM","10.00 PM","7.00 PM"};
ArrayList<Double> list = new ArrayList<Double>();
HashMap<Double,String> map = new HashMap<Double, String>();
for (int i=0;i<str.length;i++){
list.add(Double.parseDouble(str[i].split(" ")[0]));
map.put(Double.parseDouble(str[i].split(" ")[0]),str[i]);
}
System.out.println("Min >> " +map.get(Collections.min(list)));
System.out.println("Max >> "+map.get(Collections.max(list)));
答案 3 :(得分:0)
你可以使用这些吗,但你可能需要一些pre-arragments
Collections.max(arrayList);
Collections.min(arrayList);
答案 4 :(得分:0)
根据您最初填充数组值的方式,例如您从系统获得的长度,然后您可以比较它们。或者创建一个保存值部分的类,将AM或PM分别像一个标志或其他东西,然后在AM和PM之间进行排序,然后进行值。我用java日期和日历涉猎很多,只使用JodaTime。很方便!
答案 5 :(得分:0)
使用日期时间值时,通常最好使用作为日期时间值。
将字符串解析为日期时间值,收集它们,对集合进行排序,并检索集合中的第一个和最后一个元素以获得最早的&amp;最新价值观。如果需要,转换回字符串。
您可以轻松地解析字符串以创建日期时间对象。
但是请避免使用捆绑的java.util.Date&amp; Java中的日历类,因为它们是众所周知的麻烦。此外,它们总是结合日期和时间,而在您的情况下,您只有一天的时间。
在Java 8中使用Joda-Time或新的java.time package。两者都提供仅限日期的课程LocalTime
。
使用Joda-Time 2.3的示例代码。
将您的数组转换为Collection,因为我不喜欢使用数组。
String[] strings = { "6.30 AM", "6.10 AM", "10.00 PM", "7.00 PM" };
List<String> stringList = Arrays.asList( strings );
创建一个空集合,以便在我们实例化它们时收集它们。
List<LocalTime> localTimes = new ArrayList<>();
创建一个格式化程序来解析您的特定字符串格式。顺便说一句,如果您可以更改这些字符串的来源,我建议您创建24小时格式的字符串,而不使用&#34; AM / PM&#34;,类似于标准ISO 8601格式。
DateTimeFormatter formatter = DateTimeFormat.forPattern( "h'.'mm aa" );
遍历我们的字符串集合,解析每个字符串。将新的LocalTime
实例存储在集合中。
for ( String string : stringList ) {
LocalTime localTime = formatter.parseLocalTime( string );
localTimes.add( localTime );
}
对LocalTime
个对象的集合进行排序,以确定最早和最新的对象。
Collections.sort( localTimes ); // Ascending order. Earliest first, latest last.
检索最早和最新的。
LocalTime earliest = localTimes.get( 0 );
LocalTime latest = localTimes.get( localTimes.size() - 1 );
转储到控制台。
System.out.println( "localTimes: " + localTimes );
if ( !( localTimes.isEmpty() ) ) {
System.out.println( "earliest: " + formatter.print( earliest ) );
System.out.println( "latest: " + formatter.print( latest ) );
}
跑步时......
localTimes: [06:10:00.000, 06:30:00.000, 19:00:00.000, 22:00:00.000]
earliest: 6.10 AM
latest: 10.00 PM