¿Cómo puedo convertir una cadena de milisegundos a la fecha?

Tengo este codigo:

String id = c.getString("data"); 
String name = ((TextView) view.findViewById(R.id.TextView05)).getText().toString();

public static String getDate(long seconds, String dateFormat)
{
    DateFormat formatter = new SimpleDateFormat("yyyy MMMM dd HH:mm");
    long now = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(now);
    return formatter.format(calendar.getTime());
}

La opción "data" is 1341435600000. I want to have this String from milliseconds to date.

preguntado el 31 de julio de 12 a las 11:07

nope , just got 134143560000000 -

yes the o/p is correct. in which format you want to convert the string -

I tested with your code, and its working normally. It prints 2012 July 31 16:18 to me. -

6 Respuestas

try {  
      String str_date="11-June-07";
      DateFormat formatter ; 
      Date date ; 
      formatter = new SimpleDateFormat("dd-MMM-yy");
      date = (Date)formatter.parse(str_date);  
      System.out.println("Today is " +date );
    } 
catch (ParseException e)
{
System.out.println("Exception :"+e);
}

for convert milliseconds to Date

long yourmilliseconds = 1119193190;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);

Respondido 31 Jul 12, 11:07

Algo como esto:

    SimpleDateFormat ss = (SimpleDateFormat) SimpleDateFormat.getInstance();
    ss.applyPattern("yyyy MM dd HH:mm");

    // Date to String: 
    String dateToString = ss.format(new Date());

    //String to Date
    Date stringToDate = ss.parse("2012 02 16 14:10"));

Respondido 31 Jul 12, 11:07

    String dtStart = "2010-10-15T09:27:37Z";  
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

Respondido 31 Jul 12, 11:07

The same method is working fine for me. Why don't yours?

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    long cTime = System.currentTimeMillis();

    String date = getDate(cTime, "dd/MM/yyyy hh:mm:ss.SSS");

    Toast.makeText(getApplicationContext(), date, Toast.LENGTH_SHORT).show();
}

public static String getDate(long milliSeconds, String dateFormat)
{
    // Create a DateFormatter object for displaying date in specified format.
    DateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the date and time value in milliseconds to date. 
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     return formatter.format(calendar.getTime());
}

Respondido 31 Jul 12, 11:07

String id = Utils.getDate(c.getString("data"), "dd/MM/yyyy"); will be right? - user1563977

@user1563977 Si su data contains long value of time's milliseconds. That would be right. Just do that like - long cTime = c.getString("data"); Put this code instead of cTime in above answer - Praveenkumar

String id = c.getString(TAG_DATA); long milliseconds = Long.parseLong(id); String date = Utils.getDate(milliseconds, "yyyy-MM-dd HH:mm:ss"); Solved :) Another question i have :) what should i write to know what GMT is ? - user1563977

if you are not bothered about the Date format, just use it as below

String id = c.getString("data"); //"1341435600000"; 
Date date123 = new Date(Long.parseLong(id));
System.out.println(date123);

Respondido 31 Jul 12, 12:07

i need put in map.put(TAG_ID, id); - user1563977

i didn't get you. (btw, it is not mentioned in your original question) - Sunil

Code snippet to help:

String timestamp="20140809";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println("date :"+dateFormat.parse(timestamp).getTime());

Respondido el 10 de Septiembre de 14 a las 21:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.