Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java到C#-显示最近的时间_Java_C# - Fatal编程技术网

Java到C#-显示最近的时间

Java到C#-显示最近的时间,java,c#,Java,C#,不久前,我用Java编写了这段代码,其基本功能是在将当前时间与包含时间的“列表”进行比较时显示最近的时间。到目前为止,它的工作和贝娄是我所拥有的: import java.time.LocalTime; import java.util.TreeSet; TreeSet<LocalTime> times = new TreeSet<>(); times.add(LocalTime.parse("05:40")); times.add(L

不久前,我用Java编写了这段代码,其基本功能是在将当前时间与包含时间的“列表”进行比较时显示最近的时间。到目前为止,它的工作和贝娄是我所拥有的:

import java.time.LocalTime;
import java.util.TreeSet;

TreeSet<LocalTime> times = new TreeSet<>();

        times.add(LocalTime.parse("05:40"));
        times.add(LocalTime.parse("06:40"));
        times.add(LocalTime.parse("08:30"));
        times.add(LocalTime.parse("09:45"));
        times.add(LocalTime.parse("10:35"));
        times.add(LocalTime.parse("11:10"));
        times.add(LocalTime.parse("11:55"));
        times.add(LocalTime.parse("12:20"));
        times.add(LocalTime.parse("13:30"));
        times.add(LocalTime.parse("14:55"));
        times.add(LocalTime.parse("16:00"));
        times.add(LocalTime.parse("16:30"));
        times.add(LocalTime.parse("17:30"));
        times.add(LocalTime.parse("19:00"));
        times.add(LocalTime.parse("20:10"));
        times.add(LocalTime.parse("21:10"));

LocalTime ceiling = times.ceiling(LocalTime.now());
LocalTime first = times.first();

if (ceiling != null) {
            System.out.println("Bus is at: " + ceiling);
        } else {
            System.out.println("There are no more busses at this time. Next one will go in: " + first);
        }
import java.time.LocalTime;
导入java.util.TreeSet;
树集时间=新树集();
添加(LocalTime.parse(“05:40”);
添加(LocalTime.parse(“06:40”);
添加(LocalTime.parse(“08:30”);
times.add(LocalTime.parse(“09:45”);
添加(LocalTime.parse(“10:35”);
times.add(LocalTime.parse(“11:10”);
add(LocalTime.parse(“11:55”);
添加(LocalTime.parse(“12:20”);
添加(LocalTime.parse(“13:30”);
add(LocalTime.parse(“14:55”);
times.add(LocalTime.parse(“16:00”);
添加(LocalTime.parse(“16:30”);
times.add(LocalTime.parse(“17:30”);
times.add(LocalTime.parse(“19:00”);
添加(LocalTime.parse(“20:10”);
add(LocalTime.parse(“21:10”);
LocalTime天花板=times.天花板(LocalTime.now());
LocalTime first=times.first();
如果(上限!=null){
System.out.println(“总线位于:“+天花板”);
}否则{
System.out.println(“此时没有更多总线。下一个总线将进入:“+第一个”);
}
因此,它需要当前时间,将其与“时间”进行比较,并在屏幕上显示最近的时间

到目前为止,我的C版本是:

var timeOfDay = DateTime.Now.ToString("HH:mm");

        Console.WriteLine(timeOfDay);

        var time = new SortedSet<DateTime>
        {
            DateTime.Parse("05:40"),
            DateTime.Parse("06:40"),
            DateTime.Parse("08:30"),
            DateTime.Parse("09:45"),
            DateTime.Parse("10:35"),
            DateTime.Parse("11:10"),
            DateTime.Parse("11:55"),
            DateTime.Parse("12:20"),
            DateTime.Parse("13:30"),
            DateTime.Parse("14:55"),
            DateTime.Parse("16:00"),
            DateTime.Parse("16:30"),
            DateTime.Parse("17:30"),
            DateTime.Parse("19:00"),
            DateTime.Parse("20:10"),
            DateTime.Parse("21:10")
        };

        var first = time.First();
        var last = time.Last();

        Console.WriteLine("Bus is at: " + first);

        Console.WriteLine("There are no more busses at this time. Next one will go in: " + last);
var timeOfDay=DateTime.Now.ToString(“HH:mm”);
控制台写入线(一天中的时间);
var时间=新分拣数据集
{
DateTime.Parse(“05:40”),
DateTime.Parse(“06:40”),
DateTime.Parse(“08:30”),
DateTime.Parse(“09:45”),
DateTime.Parse(“10:35”),
DateTime.Parse(“11:10”),
DateTime.Parse(“11:55”),
DateTime.Parse(“12:20”),
DateTime.Parse(“13:30”),
DateTime.Parse(“14:55”),
DateTime.Parse(“16:00”),
DateTime.Parse(“16:30”),
DateTime.Parse(“17:30”),
DateTime.Parse(“19:00”),
DateTime.Parse(“20:10”),
DateTime.Parse(“21:10”)
};
var first=time.first();
var last=time.last();
控制台写入线(“总线位于:+第一位”);
WriteLine(“此时没有更多总线。下一个总线将进入:“+last”);
拼图的最后一块是显示与当前时间相比最近的时间


我的问题是:在C#中做这样一件事的最佳方法是什么?

必须进行排序。在Java中,set与TreeSet是相同的

最近时间

   var set = new SortedSet<DateTime>();
    var time = DateTime.Now;

    //2 sample values to test with
    set.Add(time.AddHours(1));
    set.Add(time.AddHours(-1));

    //We temporarily add our time to the set
    set.Add(time);
    var next = set.SkipWhile(a => !a.Equals(time)).Skip(1).FirstOrDefault();
    var previous = set.Reverse().SkipWhile(a => !a.Equals(time)).Skip(1).FirstOrDefault();

    //and remove it in the end 
    set.Remove(time);
var set=new SortedSet();
var time=DateTime.Now;
//使用2个样本值进行测试
set.Add(time.AddHours(1));
set.Add(time.AddHours(-1));
//我们暂时把时间加到布景上
设置。添加(时间);
var next=set.SkipWhile(a=>!a.Equals(time)).Skip(1.FirstOrDefault();
var previous=set.Reverse().SkipWhile(a=>!a.Equals(time)).Skip(1.FirstOrDefault();
//最后把它取下来
设置。移除(时间);

正如下面提到的Basil,对于LocalTime,您可以使用library

您是否尝试过此解决方案?请在写问题的标题时多加注意。如果你的问题是关于TreeSet系列的等价物,那么说出来。你怎么知道它是一样的?仅链接的答案通常不被接受SortedSet确实是.NET中最接近TreeSet的。更难的部分是确定与LocalTime的等价物。对于.Net上的
LocalTime
,请查看库。受Java框架、Joda Time和Java.Time的启发。