Convert Date () to String

2

I want to convert the type Long that returns new Date().getTimeInMillis() to String

The following code does not work, generates a time different from my time zone

private String formatDate(Long fechaInTypeLong) {
  Date date = new Date(fechaInTypeLong);
  SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  return formato.format(date);
}

How to convert in String according to UTC ?

    
asked by x-rw 02.11.2017 в 04:31
source

4 answers

1

To convert a long to string, you have several options:

  • Use toString ():

    new Date (). getTimeInMillis (). toString ();

  • Concatenate a string:

    (new Date (). getTimeInMillis ()) + "";

To solve the issue of not showing you the correct time, if it is the location you can try the following:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);      //En vez de ENGLISH pon el tuyo
    
answered by 02.11.2017 / 08:10
source
2

if you use .toString () it converts it to the standard format, but try with Format, for example

Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

or DateFormat

DateFormat fecha= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String aux= fechaHora.format(date);
    
answered by 02.11.2017 в 04:45
2

Try this:

return formato.format(date).toString()
    
answered by 02.11.2017 в 04:45
0

To solve the problem of the time zone use the following code:

private String formatDate(Long time) {        TimeZone tZone = TZone.getTimeZone("UTC");        Date date = new Date(time);        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");        dateFormat.setTimeZone(timeZone);        return dateFormat.format(date);    }
    
answered by 04.11.2017 в 01:57