0

我想创建一个函数,在字符串数组中显示地点的开放时间,其中第一个条目是“异常”,其他(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;
        }

}
4

2 回答 2

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);
于 2012-07-25T12:27:57.417 回答
0

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

此外,无需使用这些String.valueOf调用。如Adinia 的回答中所述,为此使用格式化程序。

我建议尽可能避免使用 java.util.Date 和 .Calendar 类。他们是出了名的麻烦。而是使用 Joda-Time 或 Java 8 中的新 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 ) );

运行时……</p>

Hours: 11:00 - 19:00

您的数据表明您的真正意图是记录没有任何日期的时间。如果是这样,请这样做。存储完整的日期时间和时区 (“Z”) 表示日期时间,将来可能会令人困惑。Joda-Time 和 Java 8 都为此目的提供了一个“LocalTime”对象。

于 2014-01-24T07:43:45.710 回答