「java转pm」class转java

博主:adminadmin 2022-12-27 14:33:07 73

本篇文章给大家谈谈java转pm,以及class转java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用Java将12小时制改为24小时制?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:mm");

System.out.println(sdf.format(new Date()));

这个是将当前时间的格式改为yyyy-MM-dd HH:dd:mm的,显示的是24小时制。

一、java代码12小时制转换24小时制方法

tr:12小时制字符串,比如8:00am,7:00pm.8:30am,6:00pm

返回值为24小时制字符串:比如18:00,20:00,21:00

ublic static String startStr(String str) {

String[] strs = str.split("--")

String total = strs[strs.length - 1]

String startHour = total.substring(0, total.indexOf(":"))

if ((total.charAt(total.indexOf("m") - 1) + "").equals("a")

(startHour.equals("12"))) {

二、java Date类型:24小时制和12小时制

String getTimestamp() {

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss sss");

Date date = new Date();

return df.format(date);

}

HH返回的是24小时制的时间

hh返回的是12小时制的时间

编写一个java程序用以将AM/PM格式的时间转换为24小时格式,求大神完成代码

SimpleDateFormat objSDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String strCurrentTime = objSDateFormat.format(Date类型的时间);

注:大写的HH为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”

这个更全

实现思路就是输入一个时间,之后会输出相应的12小时和24小时效果展示:

import java.text.SimpleDateFormat;

import java.util.Locale;

import java.util.Scanner;

public class App {

public static void main(String[] args) {

while (true) {

System.out.println("Enter time in 24-hour notation:");

Scanner sc = new Scanner(System.in);

String line = sc.nextLine();

try {

outTime(line);

} catch (TimeFormatException e) {

System.out.println("There is no such time as " + line);

System.out.println("Try again:");

continue;

}

sc = new Scanner(System.in);

line = sc.nextLine();

if ("n".equalsIgnoreCase(line)) {

break;

}

}

System.out.println("End of program");

}

public static void outTime(String line) throws TimeFormatException {

SimpleDateFormat _24time = new SimpleDateFormat("HH:mm");

SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",

Locale.ENGLISH);

try {

String[] array = line.split(":");

if (Integer.parseInt(array[0]) 0

|| Integer.parseInt(array[0]) 23) {

throw new TimeFormatException();

}

if (Integer.parseInt(array[1]) 0

|| Integer.parseInt(array[1]) 59) {

throw new TimeFormatException();

}

System.out.println(_12time.format(_24time.parse(line)));

System.out.println("Again?(y/n)");

} catch (Exception e) {

throw new TimeFormatException();

}

}

}

class TimeFormatException extends Exception {

}

java 输出时间 12小时制 要带上 am或pm

你好

SimpleDateFormat aa = new SimpleDateFormat("yyyy-MM-dd KK:mm aa",Locale.ENGLISH);

String time = aa.format(new Date());

System.out.println(time);

用这个方法,加入参数Locale.ENGLISH,既可输出am或者pm。如果加入参数Locale.CHINESE,既可输出“上午”或者“下午”

关于java转pm和class转java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。