package com.alamkanak.weekview.sample.apiclient;

import android.annotation.SuppressLint;
import android.graphics.Color;
import com.alamkanak.weekview.WeekViewEvent;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * An event model that was built for automatic serialization from json to object.
 */
public class Event {

    @Expose
    @SerializedName("name")
    private String name;
    @Expose
    @SerializedName("dayOfMonth")
    private int dayOfMonth;
    @Expose
    @SerializedName("startTime")
    private String startTime;
    @Expose
    @SerializedName("endTime")
    private String endTime;
    @Expose
    @SerializedName("color")
    private String color;

    @SuppressLint("SimpleDateFormat")
    public WeekViewEvent toWeekViewEvent() {

        // Parse time.
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date start = new Date();
        Date end = new Date();
        try {
            start = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        try {
            end = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // Initialize start and end time.
        Calendar now = Calendar.getInstance();
        Calendar startTime = Calendar.getInstance();
        startTime.setTimeInMillis(start.getTime());
        startTime.set(Calendar.YEAR, now.get(Calendar.YEAR));
        startTime.set(Calendar.MONTH, now.get(Calendar.MONTH));
        startTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        Calendar endTime = (Calendar) startTime.clone();
        endTime.setTimeInMillis(end.getTime());
        endTime.set(Calendar.YEAR, startTime.get(Calendar.YEAR));
        endTime.set(Calendar.MONTH, startTime.get(Calendar.MONTH));
        endTime.set(Calendar.DAY_OF_MONTH, startTime.get(Calendar.DAY_OF_MONTH));

        // Create a week view event.
        WeekViewEvent weekViewEvent = new WeekViewEvent(name, name, null, startTime, endTime);
        weekViewEvent.setColor(Color.parseColor(color));

        return weekViewEvent;
    }
}
convert kotlin to java code belowpackage comalamkanakweekviewsampleapiclientimport androidannotationSuppressLintimport androidgraphicsColorimport comalamkanakweekviewWeekViewEventimport comgooglegsona

原文地址: https://www.cveoy.top/t/topic/jdXa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录