How do I convert the current date into string in Java?
9 Answers
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());

- 15,331
- 2
- 27
- 26
-
Used a combination of this and the answer right below for the timestring. C: – php_coder_3809625 Jul 13 '16 at 13:54
-
@Ian Purton Is string date in UTC tz ..? – Kanagavelu Sugumar Jan 17 '17 at 08:19
-
1FYI, these troublesome classes are now legacy, supplanted by the java.time classes. See [Oracle Tutorial](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 18 '17 at 15:46
// GET DATE & TIME IN ANY FORMAT
import java.util.Calendar;
import java.text.SimpleDateFormat;
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
Taken from here

- 17,924
- 4
- 40
- 58
-
-
5I'm confused. In what universe is `Calendar` more desirable than `Date`? – jasonmp85 Jun 03 '10 at 11:24
-
1FYI, these troublesome classes are now legacy, supplanted by the java.time classes. See [Oracle Tutorial](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 18 '17 at 15:48
// On the form: dow mon dd hh:mm:ss zzz yyyy
new Date().toString();
-
58
-
2The format used by that method is terrible. Also, your KVM’s current default time zone is implicitly applied, so your results will vary. And these troublesome classes are now legacy, supplanted by the java.time classes. See [Oracle Tutorial](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 18 '17 at 15:51
Use a DateFormat
implementation; e.g. SimpleDateFormat
.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String data = df.format(new Date());

- 5,735
- 2
- 25
- 36

- 54,009
- 15
- 113
- 152
-
FYI, these troublesome classes are now legacy, supplanted by the java.time classes. See [Oracle Tutorial](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 18 '17 at 15:51
tl;dr
LocalDate.now()
.toString()
2017-01-23
Better to specify the desired/expected time zone explicitly.
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
java.time
The modern way as of Java 8 and later is with the java.time framework.
Specify the time zone, as the date varies around the world at any given moment.
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ; // Or ZoneOffset.UTC or ZoneId.systemDefault()
LocalDate today = LocalDate.now( zoneId ) ;
String output = today.toString() ;
2017-01-23
By default you get a String in standard ISO 8601 format.
For other formats use the java.time.format.DateTimeFormatter
class.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154
Faster :
String date = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis( ));

- 405
- 14
- 25
Most of the answers are/were valid. The new JAVA API modification for Date handling made sure that some earlier ambiguity in java date handling is reduced.
You will get a deprecated message for similar calls.
new Date() // deprecated
The above call had the developer to assume that a new Date object will give the Date object with current timestamp. This behavior is not consistent across other Java API classes.
The new way of doing this is using the Calendar Instance.
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()
Here too the naming convention is not perfect but this is much organised. For a person like me who has a hard time mugging up things but would never forget something if it sounds/appears logical, this is a good approach.
This is more synonymous to real life
- We get a Calendar object and we look for the time in it. ( you must be wondering no body gets time from a Calendar, that is why I said it is not perfect.But that is a different topic altogether)
- Then we want the date in a simple Text format so we use a SimpleDateFormat utility class which helps us in formatting the Date from Step 1. I have used yyyy, MM ,dd as parameters in the format. Supported date format parameters
One more way to do this is using Joda time API
new DateTime().toString("yyyy-MM-dd")
or the much obvious
new DateTime(Calendar.getInstance().getTime()).toString("yyyy-MM-dd")
both will return the same result.

- 21
- 6
-
FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), `java.util.Calendar`, and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Also, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to java.time. – Basil Bourque May 08 '17 at 20:55
-
1@BasilBourque That is what I meant in my answer, that the top voted answer is right but not relevant to a present day developer. Finally java will have a Clock class and Clock.getTime() would be much more sensible and real life imitating than Date or DateTime or Time classes. Java developers need to know that OOPS started as something which imitates real life scenarios. – Swapnil Vargaonkar May 12 '17 at 08:46
-
FYI, the `Clock` implementation in Java 8 is exactly the same as the legacy classes. Only in Java 9 does a new implementation arrive, capable of capturing the current moment with a resolution as fine as nanoseconds. – Basil Bourque Aug 18 '17 at 15:56
-
Yes that is the reason I wrote "finally" Java will have a Clock class which will imitate the real life scenario( OOPS concept) and show/capture( the way you would prefer saying it) the current moment. – Swapnil Vargaonkar Aug 21 '17 at 13:42
For time as YYYY-MM-dd
String time = new DateTime( yourData ).toString("yyyy-MM-dd");
And the Library of DateTime is:
import org.joda.time.DateTime;
-
And what library is `DateTime` from? An Answer on Stack Overflow should have some explanation as this site is meant to be more than a snippet library. – Basil Bourque Aug 18 '17 at 15:45
public static Date getDateByString(String dateTime) {
if(dateTime==null || dateTime.isEmpty()) {
return null;
}
else{
String modified = dateTime + ".000+0000";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date dateObj = new Date();
Date dateObj1 = new Date();
try {
if (dateTime != null) {
dateObj = formatter.parse(modified);
}
} catch (ParseException e) {
e.printStackTrace();
}
return dateObj;
}
}

- 9
- 2
-
1Some explanations about this piece of code would probably be beneficial. – Pyves Jun 13 '17 at 19:30
-
1The answer is not relevant to the question. And No, you can't pass the date in any `String` format to your function. – Jerin Joseph Jun 13 '17 at 19:30