java+date+setdate替代_Java DateFormat setLenient()用法及代码示例

DateFormat类中的setLenient(boolean leniency)方法用于指定此DateFormat对象的日期和时间的解释是否宽松。

用法:

public void setLenient(boolean leniency)

参数:该方法采用一种布尔类型的参数leniency,该leniency引用了DateFormat对象的日历模式。布尔值true将打开宽大处理模式,而false则关闭宽大处理模式。

返回值:该方法不返回任何值。

以下示例程序旨在说明Calendar类的setLenient()方法的用法:

示例1:

// Java code to illustrate

// setLenient() method

import java.text.*;

import java.util.*;

public class DateFormat_Demo {

public static void main(String[] args)

{

// Initializing the first formatter

DateFormat DFormat

= DateFormat.getDateTimeInstance();

System.out.println("Object: " + DFormat);

// String formatting

String str = DFormat.format(new Date());

// Displaying the string time

System.out.println(str);

System.out.println("Leniency: "

+ DFormat.isLenient());

// Changing the leniency

DFormat.setLenient(false);

// Displaying the modified leniency

System.out.println("New Leniency: "

+ DFormat.isLenient());

}

}

输出:

Object: java.text.SimpleDateFormat@7945516e

Mar 28, 2019 6:03:48 PM

Leniency: true

New Leniency: false

示例2:

// Java code to illustrate

// setLenient() method

import java.text.*;

import java.util.*;

public class DateFormat_Demo {

public static void main(String[] args)

{

// Initializing the first formatter

DateFormat DFormat

= DateFormat.getDateInstance();

System.out.println("Object: " + DFormat);

// String formatting

String str = DFormat.format(new Date());

// Displaying the string time

System.out.println(str);

System.out.println("Leniency: "

+ DFormat.isLenient());

// Changing the leniency

DFormat.setLenient(false);

// Displaying the modified leniency

System.out.println("New Leniency: "

+ DFormat.isLenient());

}

}

输出:

Object: java.text.SimpleDateFormat@ce9bf0a5

Mar 28, 2019

Leniency: true

New Leniency: false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值