How can I get current time and day in Java? I'd like to name files based on day and time.开发者_JAVA技巧
For example if:
Day : march 25th, 1993
Hour : 12.20 pm
then the file name should be: 250319931220
Is this something I as a beginner can achieve?
Thank you
To get the current date, use java.util.Date
.
Date now = new Date();
To format it in a human friendly string representation, use java.text.SimpleDateFormat
.
String name = new SimpleDateFormat("ddMMyyyyHHmm").format(now);
Click the above SimpleDateFormat
link to see all available format patterns.
Alternatively, you can also just get the current timestamp by System#currentTimeMillis()
.
long now = System.currentTimeMillis();
See also:
- Java tutorial - Customizing date and time formats
I would do something like this:
Calendar now = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("ddMMyyyyHHmm");
String result = df.format(now.getTime());
Lots of methods on java.util.Date are deprecated, it is generally preferred to access the current time through the Calendar class.
Check the javadoc for SimpleDateFormat to refine the output to get the exact String you need.
Check out the Date
class. random google example here: http://www.java-examples.com/simple-java-date-example
You need Date
and SimpleDateFormat
. This should do what you're looking for:
new SimpleDateFormat("yyyyMMddHHmm").format(new Date())
You can use Java's Calendar class to get this information. More information on Calendar is available here
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html
To do what you want - you'd do something like this:
Calendar now = Calendar.getInstance(); // Gives you the current time
String fileName = now.get(Calendar.DAY_OF_MONTH) + now.get(Calendar.MONTH) + now.get(Calendar.YEAR) + now.get(Calendar.HOUR) + now.get(Calendar.MINUTE);
Another option, besides Date
, would be to use Calendar
, which seems to be the non-deprecated replacement for Date
. It can let you query a whole bunch about different pieces of information, from which you should be able to construct your string.
Out of curiosity, are you sure that you want to store files with this structure? It seems that using some other format (perhaps the number of seconds since the start of the UNIX epoch) might be easier to parse.
EDIT: To clarify, Date
itself isn't deprecated; rather most of the methods you'd use to extract formatted data from it is. Thanks to commenters for pointing this out!
My answer assumes you agree it's best to put the year first, in year-month-date order for chronological sorting alphabetically. If not, twiddle with the format codes for your way.
Using the third-party library Joda-Time 2.3 rather than the notoriously bad java.util.Date/Calendar classes.
If you are mixing these files from different computers that may be in a different time zone, or a computer that is moving between time zones, it may be best to record the UTC/GMT date-time (no time zone offset, 'Z' on end signifies Zulu time).
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
DateTime now = new DateTime( DateTimeZone.forID( "America/Vancouver" ) );
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmss");
System.out.println( "now in local terms: " + formatter.print( now ) );
DateTimeFormatter formatterUtcIso = ISODateTimeFormat.dateHourMinute().withZoneUTC();
//DateTimeFormatter formatterUtcIso = ISODateTimeFormat.basicDateTimeNoMillis().withZoneUTC();
System.out.println( "now in UTC for ISO: " + formatterUtcIso.print( now ) );
DateTimeFormatter formatterUtcIsoSpaced = DateTimeFormat.forPattern("yyyy-MM-dd HH-mm-ss'Z'").withZone( DateTimeZone.UTC );
System.out.println( "now in UTC for ISO with a space: " + formatterUtcIsoSpaced.print( now ) );
When run…
now in local terms: 20131223203714
now in UTC for ISO: 2013-12-24T04:37
now in UTC for ISO with a space: 2013-12-24 04-37-14Z
精彩评论