How to convert current date to string in java?

How to convert current date to string in Java?

+81
java string date formatting
May 31 '10 at 10:20
source share
9 answers
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); 
+162
Feb 21
source share

 // 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

+55
May 31 '10 at 10:23 a.m.
source share
 // On the form: dow mon dd hh:mm:ss zzz yyyy new Date().toString(); 
+23
May 31 '10 at 10:22
source share

Use DateFormat ; e.g. SimpleDateFormat .

 DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); String data = df.format(new Date()); 
+11
May 31 '10 at 10:22
source share

Faster:

 String date = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis( )); 
+8
Jul 11 '13 at 17:03
source share

TL; DR

 LocalDate.now() .toString() 

2017-01-23

It is better to explicitly indicate the desired / expected time zone.

 LocalDate.now( ZoneId.of( "America/Montreal" ) ) .toString() 

java.time

The modern approach to Java 8 and later consists of the java.time framework.

Specify the time zone, as the date changes worldwide at any time.

 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 the 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 supersede the nasty old time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to java.time.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.

Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport and then adapted to Android in ThreeTenABP (see How to use ... ).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter , etc.

+1
Apr 19 '16 at 7:51
source share

Time in the format YYYY-MM-dd

 String time = new DateTime( yourData ).toString("yyyy-MM-dd"); 

And the DateTime Library:

import org.joda.time.DateTime;

+1
Aug 18 '17 at 13:27
source share

Most of the answers are / valid. A new modification of the JAVA API for date processing has helped to reduce some earlier ambiguity in java date processing.

You will receive an outdated message for similar calls.

 new Date() // deprecated 

The above call made the developer assume that the new Date object would provide a Date object with the current timestamp. This behavior is incompatible with other Java API classes.

A new way to do this is to use the Calendar instance.

 new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime() 

Here, the naming convention is not perfect, but it is very organized. For a person like me who has a hard time pushing things, but never forgets something if it sounds / seems logical, this is a good approach.

It looks more like real life

  • We get a Calendar object, and we look for time in it. (You should be wondering that no body receives time from the Calendar, so I said that this is not ideal. But this is another topic in general)
  • Then we need the date in simple text format, so we use the SimpleDateFormat utility class, which helps us format the date from step 1. I used yyyy, MM, dd as the parameters in the format. Supported Date Format Options

Another way to do this is to use the Joda Time API

 new DateTime().toString("yyyy-MM-dd") 

or a lot of obvious

 new DateTime(Calendar.getInstance().getTime()).toString("yyyy-MM-dd") 

both will return the same result.

0
May 6 '17 at 10:52 a.m.
source share
 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; } } 
-2
Jun 13 '17 at 18:59
source share



All Articles