Android 获取日期并插入到文件名

发布于 2024-12-16 22:02:39 字数 248 浏览 2 评论 0原文

我有一个非常烦人的问题。我想获取当前日期/时间并将其插入文件名中,但我一生都无法让它工作。

我想获取 2011-11-18 12:13:57 的时间,然后将其插入到我的文件名

filename-2011-11-18-12:13:57.tar.gz

我已经尝试过 SimpleDateFormat 等,但它就是行不通!

(我正在用 Eclipse Indigo 编写)

I am having a quite annoying problem. I want to get the current date/time and insert it into a filename but I can't for my life get it to work.

I want to get the time as 2011-11-18 12:13:57 and then insert it into my filename

filename-2011-11-18-12:13:57.tar.gz

I have tried SimpleDateFormat etc. but it just won't work!

(I am writing in Eclipse Indigo)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

却一份温柔 2024-12-23 22:02:39

您可以使用这个:

import java.text.SimpleDateFormat;
// ...

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date now = new Date();
String fileName = formatter.format(now) + ".tar.gz";

此外,您一定在某个地方遇到错误,这对查找问题有很大帮助。确保没有空的 catch 块:

catch (Exception e) {}

You can use this:

import java.text.SimpleDateFormat;
// ...

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date now = new Date();
String fileName = formatter.format(now) + ".tar.gz";

Also you must be getting an error somewhere, that would help a lot to find the problem. Make sure you don't have empty catch blocks :

catch (Exception e) {}
抚笙 2024-12-23 22:02:39

使用它来获取您想要的日期时间:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String timeStamp = dateFormat.format(date.toLocaleString());

您可以像这样创建文件:

FileOutputStream out = context.openFileOutput(timeStamp , context.MODE_PRIVATE);
out.write(string.getBytes());
out.close();

Use this to get the date time you want :

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String timeStamp = dateFormat.format(date.toLocaleString());

and you can create your file like this :

FileOutputStream out = context.openFileOutput(timeStamp , context.MODE_PRIVATE);
out.write(string.getBytes());
out.close();
爱,才寂寞 2024-12-23 22:02:39

我希望这可以帮助您获取当前日期和时间

如何使用 SimpleDateFormat 显示当前日期?

I hope this could help u out to get the current date and time

How to use SimpleDateFormat to show the current Date?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文