无法更改12:00之后(下午)的时间的AM/PM。

huangapple go评论73阅读模式
英文:

Cannot change AM/PM of Time After 12:00 (AfterNoon)

问题

public class PrayerActivity extends AppCompatActivity {
    TextView result;
    String fetch = "12:43";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = findViewById(R.id.result);
        Date dateCode = null;
        String formatTwelve;
        String resultsAfter;
        final SimpleDateFormat code12Hours = new SimpleDateFormat("hh:mm");
        try {
            dateCode = code12Hours.parse(fetch);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        formatTwelve = code12Hours.format(dateCode);
        if (formatTwelve.equals(fetch)) {
            resultsAfter = formatTwelve + " AM";
        } else {
            resultsAfter = formatTwelve + " PM";
        }
        result.setText(resultsAfter);
    }
}
英文:

How do i convert 24 hours format to 12 hours along with AM/PM, i have checked articles and also in stackoverflow but problem is if string is "12:43" (afternoon time) and i converted it into 12 hours then it shows "12:43 AM" but it should show "12:43 PM". Below is the java code which i have written.

> MainActivity.Java

public class PrayerActivity extends AppCompatActivity {
    TextView result;
String fetch="12:43"
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
result=findViewById(R.id.result);
Date dateCode = null;
String formatTwelve;
String resultsAfter;
final SimpleDateFormat code12Hours = new SimpleDateFormat("hh:mm");
                            try {
                                dateCode = code12Hours.parse(fetch);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
formatTwelve = code12Hours.format(dateCode);
if (formatTwelve.equals(fetch)) {
                                resultsAfter= formatTwelve + " AM";
                            } else {
                                resultsAfter= formatTwelve + " PM";
                            }
result.setText(resultsAfter);
}}

答案1

得分: 3

public static void main(String[] args) {
    String[] stringDates = { "12:43", "14:00", "10:00" };
    for (String dateString : stringDates) {
        // Input 24 HRS
        LocalTime localTime = LocalTime.parse(dateString, DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH));

        // Output 12 hrs
        System.out.println(localTime.format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH)));
    }
}

output

12:43 PM
02:00 PM
10:00 AM
英文:
public static void main(String[] args) {
		String[] stringDates = { "12:43", "14:00", "10:00" };
		for (String dateString : stringDates) {
			// Input 24 HRS
			LocalTime localTime = LocalTime.parse(dateString, DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH));

			// Output 12 hrs
			System.out.println(localTime.format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH)));
		}
	}

output

12:43 PM
02:00 PM
10:00 AM

答案2

得分: 1

以下是翻译好的部分:

你只需要使用 "h:mm a""hh:mm aa" 来获取更多细节或者代码,你可以参考这个 gist 链接以便更好地理解。

try {
    String timeLong = "2020-10-08 06:06:30";
    String timeShort = "16:06 AM";
    SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
    Log.v("out", formatShort.format(formatLong.parse(timeLong)));
    Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
    e.printStackTrace();
}

输出:
06:06 AM
04:06 PM

英文:

all you have to do is use "h:mm a" or "hh:mm aa" for more details or code you can follow this gist link for better understanding.

try {
    String timeLong = "2020-10-08 06:06:30";
    String timeShort = "16:06 AM";
    SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
    Log.v("out", formatShort.format(formatLong.parse(timeLong)));
    Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
    e.printStackTrace();
}

Output :
06:06 AM
04:06 PM

huangapple
  • 本文由 发表于 2020年10月8日 16:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64259117.html
匿名

发表评论

匿名网友

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

确定