java生成不带时间日期
时间: 2023-11-17 16:06:24 浏览: 244
在Java中,可以使用SimpleDateFormat类来生成不带时间日期的字符串。以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(new Date());
System.out.println(formattedDate);
}
}
```
这段代码将输出当前日期,格式为“年-月-日”,不包含时间部分。
相关问题
Java 1.6 再sftp 上每天生成一个带有当前时间戳的文件夹
在Java 1.6中,你可以使用JSch库来连接SFTP服务器并创建一个新的目录,这个目录包含当天的时间戳。首先,需要在项目中添加JSch依赖。以下是一个简单的示例代码片段,展示了如何实现这一功能:
```java
import com.jcraft.jsch.ChannelSftp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SftpDailyDirectory {
private static final String USER = "your_username";
private static final String PASS = "your_password";
private static final String HOST = "your_sftp_host";
private static final int PORT = 22; // 默认SSH端口
private static final String REMOTE_DIR = "/path/to/remote/directory"; // 远程目录路径
public static void main(String[] args) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(USER, HOST, PORT);
session.setPassword(PASS);
session.setConfig("StrictHostKeyChecking", "no"); // 配置不检查主机密钥,生产环境中应谨慎处理
session.connect(); // 连接SFTP服务器
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
// 创建日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDate = sdf.format(new Date()); // 获取当前日期时间戳
// 在远程目录下创建新的文件夹,名字为时间戳
File remoteFolder = new File(REMOTE_DIR + "/" + currentDate);
channel.mkdir(remoteFolder.getAbsolutePath());
System.out.println("Successfully created directory at " + remoteFolder.getAbsolutePath() + " on SFTP server.");
channel.disconnect();
session.disconnect();
} catch (JSchException | IOException e) {
e.printStackTrace();
}
}
}
```
在这个例子中,你需要替换`USER`, `PASS`, `HOST`, 和 `REMOTE_DIR`为你实际的SFTP配置信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)