public class DateTool { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public DateTool() { } public static String dateToString(Date date) { return sdf.format(date); } public static Date stringToDate(String dateStr) { try { return sdf.parse(dateStr); } catch (ParseException var2) { var2.printStackTrace(); return null; } } }
时间: 2024-02-14 16:36:03 浏览: 30
这段代码是一个Java类,提供了两个静态方法,用于将日期对象和日期字符串相互转换。具体来说,类中定义了一个私有静态的SimpleDateFormat对象sdf,用于指定日期格式。其中dateToString方法接收一个Date类型的参数,将其转换为指定格式的日期字符串,并返回该字符串;stringToDate方法接收一个日期字符串参数,将其转换为Date类型的日期对象,并返回该对象。如果在转换日期字符串为Date对象时发生异常,则会打印异常堆栈信息并返回null。该类的使用可以方便地将日期对象和日期字符串在不同的场合进行转换。
相关问题
优化这段代码,使其结果就显示一个import java.text.SimpleDateFormat; import java.util.Date; class Timer implements Runnable { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void run() { while (true) { System.out.print(sdf.format(new Date()) + " "); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Watch { public static void main(String[] args) { Thread thread = new Thread(new Timer()); thread.start(); } }
可以使用Java的System类提供的控制台清屏方法clear(),以及光标移动到开头的方法setCursorPosition(),使得输出结果只显示一行。代码如下:
```
import java.text.SimpleDateFormat;
import java.util.Date;
class Timer implements Runnable {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void run() {
while (true) {
System.out.print("\033[H\033[2J"); // 清屏
System.out.flush();
System.out.print(sdf.format(new Date()) + " ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Watch {
public static void main(String[] args) {
Thread thread = new Thread(new Timer());
thread.start();
}
}
```
注意:该方法只在Linux和MacOS等Unix系统以及支持ANSI控制字符的Windows终端环境下可用。在一些Windows终端环境下可能无法正常工作。
优化这段代码,使其运行结果只显示一行import java.text.SimpleDateFormat; import java.util.Date; class Timer implements Runnable { public void run() { while(true) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())); try { Thread.sleep(1000); } catch(InterruptedException e) {e.printStackTrace(); } } } } class Watch { public static void main(String[] args) { Thread thread=new Thread(new Timer()); thread.start(); } }
可以将实例化SimpleDateFormat对象和输出语句移到while循环外,避免每次循环都创建对象,同时使用System.out.print()代替System.out.println(),使输出不换行。代码如下:
```
import java.text.SimpleDateFormat;
import java.util.Date;
class Timer implements Runnable {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void run() {
while (true) {
System.out.print(sdf.format(new Date()) + " ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Watch {
public static void main(String[] args) {
Thread thread = new Thread(new Timer());
thread.start();
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)