是用switch 语句编写一个JAVA应用程序,程序将根据系统时间来判断当前月份在哪一个季节,

是用switch 语句编写一个JAVA应用程序,程序将根据系统时间来判断当前月份在哪一个季节,,第1张

package zhidao;

import javautilCalendar;

public class Month {

public static void main(String[] args){

Calendar c=CalendargetInstance();

int month = cget(CalendarMONTH) + 1;

if(month >= 1 && month <=3){

Systemoutprintln("现在是" + month + "月");

Systemoutprintln("现在是春天");

}else if(month <= 6){

Systemoutprintln("现在是" + month + "月");

Systemoutprintln("现在是夏天");

}else if(month <= 9){

Systemoutprintln("现在是" + month + "月");

Systemoutprintln("现在是秋天");

}else if(month <= 12){

Systemoutprintln("现在是" + month + "月");

Systemoutprintln("现在是冬天");

}else{

Systemoutprintln("系统时间错误");

}

}

}

java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用dateformat类或者simpledateformat类来判断是否是正确的日期格式。下面的例子供参考。更详细的内容(比如yyyy,mm,dd各代表什么)可以参考javadoc。

public

class

dateutil

{

private

static

final

simpledateformat

dateformat

=

null;

static

{

//

指定日期格式为四位年/两位月份/两位日期,注意yyyy/mm/dd区分大小写;

dateformat

=

new

simpledateformat("yyyy/mm/dd");

//

设置lenient为false

否则simpledateformat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01

dateformatsetlenient(false);

}

public

static

boolean

isvaliddate(string

s)

{

try

{

dateformatparse(s);

return

true;

}

catch

(exception

e)

{

//

如果throw

javatextparseexception或者nullpointerexception,就说明格式不对

return

false;

}

}

//

下面这个方法则可以将一个日期按照你指定的格式输出

public

static

string

formatdate(date

d)

{

return

dateformatformat(d);

}

}

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Date dt = new Date();

fxsetDate(dfparse(dfformat(dt)));

不过这样做有一个问题就是,即使前面将Date型格式化,存到实体类型里的Date仍然不带有格式,直接打印实体的Date型会默认直接调用Date类的toString方法,而Date类的toString方法默认格式是dow mon dd hh:mm:ss zzz yyyy,所以打出的格式会与你想存入的不符合,其实就相当于直接将dt存入了实体中。如果想取出带格式的除非再进行转化,否则得话还是建议把属性改成String型的。

以上就是关于是用switch 语句编写一个JAVA应用程序,程序将根据系统时间来判断当前月份在哪一个季节,全部的内容,包括:是用switch 语句编写一个JAVA应用程序,程序将根据系统时间来判断当前月份在哪一个季节,、java的date类,得到两位月份的方法如01月、在java中如何获得当前年份月份时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9813802.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存