什么是在安卓中表示时间的最佳方式?

huangapple 未分类评论53阅读模式
英文:

What is the best way to represent time in android?

问题

public void getMondayGroup(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(DBUtil.MONDAY_TABLE,
            new String[]{DBUtil.KEY_ID,
                    DBUtil.KEY_GROUP_NAME,
                    DBUtil.KEY_GROUP_DAY,
                    DBUtil.KEY_START_TIME,
                    DBUtil.KEY_END_TIME},
            DBUtil.KEY_ID + "=?",
            new String[]{String.valueOf(id)},null,null,null,null);

    if (cursor != null) {
        cursor.moveToFirst();
    }

    Groups groups = new Groups();
    if(cursor != null) {
        groups.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_ID))));
        groups.setGroupName(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_GROUP_NAME)));
        groups.setGroupDay(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_GROUP_DAY)));
        groups.setGroupStartTime(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_START_TIME)));
        groups.setGroupEndTime(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_END_TIME)));

        // Issue
        // 将以下行:
        // String local = local_time.MIN.plus(Duration.ofMinutes(260L)).toString();
        // 替换为:
        LocalTime localTime = LocalTime.parse(groups.getGroupStartTime()).plus(Duration.ofMinutes(260L));
        String local = localTime.toString();

        // I want to replace this date with time
        // 我想将这个日期替换为时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a", Locale.US);
        String formattedTime = dateFormat.format(new Date(cursor.getLong(
                cursor.getColumnIndex(DBUtil.KEY_START_TIME))));
        groups.setDateCreated(formattedTime);
    }
}
英文:

I'm a volunteer trying to create an app for a local organization that assists people with recovery, employment, support, and training called REST please help me. I created this class to represent time as a prototype. What I need is how to represent an example such as: 1:45 PM - 2:35 PM, 8 AM - 8:30 AM, and so on. I need this solution to work on minimumSdk 21 as Duration requires api 26 and that will not do because the app will not be compatible with most device.

import java.time.LocalTime;
import java.time.Duration;

class JavaTime {
	private String local;

	public JavaTime() {
	}

	public static void main(String[] args) {

		JavaTime java_time = new JavaTime();

		java_time.local = LocalTime.MIN.plus(Duration.ofMinutes(260L)).toString();
		System.out.println(java_time.local);
	}
}

Output:4:20

Here is the method I need to finish in the database:

public void getMondayGroup(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(DBUtil.MONDAY_TABLE,
                new String[]{DBUtil.KEY_ID,
                        DBUtil.KEY_GROUP_NAME,
                        DBUtil.KEY_GROUP_DAY,
                        DBUtil.KEY_START_TIME,
                        DBUtil.KEY_END_TIME},
                DBUtil.KEY_ID + "=?",
                new String[]{String.valueOf(id)},null,null,null,null);

        if (cursor != null) {
            cursor.moveToFirst();
        }

        Groups groups = new Groups();
        if(cursor != null) {
            groups.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_ID))));
            groups.setGroupName(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_GROUP_NAME)));
            groups.setGroupDay(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_GROUP_DAY)));
            groups.setGroupStartTime(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_START_TIME)));
            groups.setGroupEndTime(cursor.getString(cursor.getColumnIndex(DBUtil.KEY_END_TIME)));

            // Issue
            String local = local_time.MIN.plus(Duration.ofMinutes(260L)).toString();

            // I want to replace this date with time
            DateFormat dateFormat = DateFormat.getDateInstance();
            String formattedDate = dateFormat.format(new Date(cursor.getLong(
                    cursor.getColumnIndex(DBUtil.KEY_START_TIME)).getTime()); // Feb 23, 2020
            groups.setDateCreated(formattedDate);*/
        }
    }

huangapple
  • 本文由 发表于 2020年4月7日 10:38:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/61071958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定