开放时间日期功能显示错误的时间

时间:2012-07-25 09:11:44

标签: java android date calendar

我想创建一个显示字符串阵列中场所开放时间的函数,其中第一个条目是“例外”,其他条目(1-7)显示“day opentime:closetime”

但显示错误的开放时间!
我展示了对象中的一个数据:

"created_at": "2012-01-03T13:31:53Z",
    "updated_at": "2012-03-02T11:08:36Z",
    "start_timestamp": 1325376000,
    "exception_nl": "",
    "exception_en": "",
    "id": 447,
    "end": "2012-12-31T00:00:00Z",
    "enabled": true,
    "start": "2012-01-01T00:00:00Z",
    "season_openings": [
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Monday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3123,
            "day": "Maandag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Tuesday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3124,
            "day": "Dinsdag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Wednesday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3125,
            "day": "Woensdag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Thursday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3126,
            "day": "Donderdag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Friday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3127,
            "day": "Vrijdag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Saturday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3128,
            "day": "Zaterdag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        },
        {
            "endtime_timestamp": 946749600,
            "created_at": "2012-01-03T13:31:53Z",
            "endtime": "2000-01-01T18:00:00Z",
            "closed": false,
            "day_en": "Sunday",
            "updated_at": "2012-03-02T11:08:36Z",
            "id": 3129,
            "day": "Zondag",
            "starttime": "2000-01-01T10:00:00Z",
            "starttime_timestamp": 946720800
        }

你可以非常清楚地看到有一个“季节”,开放时间是有效的(在这种情况下是整年),然后是7个显示日期的对象。开始时间和结束时间是我需要的时间,这是标准日期。日,年,月都无关紧要。

但是当我运行代码时,我的输出是:

07-25 10:46:17.720: I/openingstijden(31338): Maandag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Dinsdag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Woensdag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Donderdag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Vrijdag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Zaterdag: 21:50 - 21:50
07-25 10:46:17.720: I/openingstijden(31338): Zondag: 21:50 - 21:50

(在这里,我还没有把这些日子包括在内,对于荷兰人来说很抱歉,但这意味着周一到周日,所以这很好) 但所有的时间都错了。我不知道21:50是如何突然出现的。有人有想法吗? 下面是我的代码:

public String[] GetOpeningHours(JVenue venue, String language){
        Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
        String[] oh= new String[8];
        Date start,end;


        JVenueSeasons seizoen = venue.venue_seasons.get(0); // get first, second ALWAYS enabled:false

        if(language.equals("nld")){
        oh[0]=seizoen.exception_nl; //exception eerste string uit array
        for (int i=0; i<7;i++){
            if(seizoen.season_openings.get(i).closed==true){
                oh[(i+1)]= "gesloten";
            }else{
            start=seizoen.season_openings.get(i).starttime;
            end=seizoen.season_openings.get(i).endtime;
            calendar.setTime(start);
            String starttijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));
            calendar.setTime(end);
            String eindtijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));

            oh[(i+1)]= seizoen.season_openings.get(i).day.substring(0, 2)+ ": " + starttijd +" - "+eindtijd;
            }
        }
        }else{
            oh[0]=seizoen.exception_en; //exception eerste string uit array
            for (int i=0; i<7;i++){
                if(seizoen.season_openings.get(i).closed==true){
                    oh[(i+1)]= "closed";
                }else{
                start=seizoen.season_openings.get(i).starttime;
                end=seizoen.season_openings.get(i).endtime;
                calendar.setTime(start);
                String starttijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));
                calendar.setTime(end);
                String eindtijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));
                oh[(i+1)]= seizoen.season_openings.get(i).day_en.substring(0, 2)+ ": " + starttijd +" - "+eindtijd;
                }
            }   
        }
        return oh;
        }

}

2 个答案:

答案 0 :(得分:0)

在您的代码中,您有:

calendar.setTime(start);
String starttijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));
calendar.setTime(end);
String eindtijd= String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))+":"+String.valueOf(calendar.get(Calendar.MINUTE));

我不太确定,但似乎calendar.setTime()函数没有按预期工作,并且日历仍然有固有的日期和时间值,您设置为开始和结束变量。

尝试使用SimpleDateFormat,例如:

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
String starttijd = timeFormat.format(start);
String eindtijd = timeFormat.format(end);

答案 1 :(得分:0)

我也不确定,但是您的代码似乎采用了ISO 8601字符串,例如“2000-01-01T10:00:00Z”并将其分配给java.util.Date。我没有看到你在哪里实例化Date对象。和Adinia一样,我猜不到你在某个时候用执行时的当前日期时间(运行时)实例化了一个Date。

此外,无需使用String.valueOf次呼叫。使用格式化程序,如the answer by Adinia中所述。

我建议尽可能避免java.util.Date&amp; .Calendar类。众所周知,它们很麻烦。而是在Java 8中使用Joda-Time或新的java.time。*类。

Joda-Time中,您只需将ISO 8601字符串传递给DateTime上的构造函数。

以下是使用Joda-Time 2.3的一些示例代码。我使用阿姆斯特丹时区来调整数据的日期时间值。如果要显示UTC / GMT,请将常量“DateTimeZone.UTC”传递给构造函数。

String endtime = "2000-01-01T18:00:00Z";
String starttime = "2000-01-01T10:00:00Z";

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Amsterdam" ); // Or: DateTimeZone.UTC

DateTime end = new DateTime( endtime, timeZone );
DateTime start = new DateTime( starttime, timeZone );

DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");

System.out.println( "Hours: " + formatter.print(  start ) + " - " + formatter.print( end ) );

跑步时......

Hours: 11:00 - 19:00

您的数据表明您的真实意图是记录没有任何日期的时间。如果是这样,那就这样做。存储完整的日期时间&amp;时区(“Z”)表示日期时间,可能会在将来引起混淆。 Joda-Time&amp; Java 8都为此提供了一个“LocalTime”对象。