Cadena de conversión de Java, que tiene un objeto de milisegundos hasta la fecha

I have 2 Strings

  • 2012-06-25 15:02:22.948
  • + 0530

I need a new string which adds the 5:30 to the time in the first string.

I thought I can do this by converting both strings to date objects and then adding. But i dont know how to do it, as when i use

yyyy MM dd hh:mm:ss as the date format for the first string, I get an error.


preguntado el 04 de julio de 12 a las 08:07

But i am not able to parse my string properly to a date object. I think its because of the 22.948, the decimal point -

they are need to use yyyy MM dd hh:mm:ss.SSS as format -

The previous comment is a good way to start. Also you get an error because your first string is in "yyyy-MM-dd hh:mm:ss.SSS" format, not the one you provided. -

Also your second string seems to represent timezone...this specifies IST timezone..I hope that clears you a way to get desired output -

5 Respuestas

The format of the string 2012-06-25 15:02:22.948 no está yyyy MM dd hh:mm:ss, so it's not surprising that you get "an error" (what error is it? the more specific you are, the better people can help you!).

Trata yyyy-MM-dd HH:mm:ss.SSS. Consulte las Documentación API de SimpleDateFormat to understand the exact syntax of the format string.

Note: Upper and lower case is important in the format string. hh means 12-hour clock, HH means 24-hour clock. If you use hh, analizando 15 for the hours won't work. You also didn't include the milliseconds SSS en la cadena de formato.

Respondido 04 Jul 12, 08:07

You can merge both you string String1+string2 and can use format yyyy-MM-dd HH:mm:ss.SSSZ to parse the date. You can see more documentation aquí

Respondido 04 Jul 12, 09:07

You're getting an exception because the your date format String is wrong. You're giving a date string on the form

"yyyy-MM-dd hh:mm:ss.S"

Ver formato de fecha simple javadoc

Respondido 04 Jul 12, 08:07

Prueba esto:

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    Date date = format.parse("2012-06-25 15:02:22.948");
    Calendar calendar = new GregorianCalendar();

    int time = Integer.parseInt("0530");
    int hour = time / 100;
    int minute = time % 100;

    calendar.add(Calendar.HOUR_OF_DAY, hour);
    calendar.add(Calendar.MINUTE, minute);

    String newDateInString = format.format(calendar.getTime());

Respondido 04 Jul 12, 09:07

Las otras respuestas son correctas pero desactualizadas.


The old date-time classes (java.util.Date/.Calendar etc.) bundled with the earliest versions of Java are now legacy.

Those old classes have been supplanted by the java.tiempo paquete. Ver Tutorial de Oracle. Gran parte de la funcionalidad se ha actualizado a Java 6 y 7 en ThreeTen-Backport y más adaptado a Android en TresDiezABP.


El LocalDateTime class represent a date-time without time zone. Use those for the first piece.

Your format is close to standard ISO 8601 format, just replace the SPACE with a T.

String input = "2012-06-25 15:02:22.948";
String inputStandardized = input.replace( " " , "T" );
LocalDateTime ldt = LocalDateTime.parse( inputStandardized );

Offset from UTC

The other piece is the desplazamiento-de-UTC. Utilizamos el ZoneOffset clase para esto.

ZoneOffset offset = ZoneOffset.of( "+0530" );

Without an offset or time zone the LocalDateTime is no an actual moment on the timeline but rather a rough idea about a possible moment. Now we add your desplazamiento-de-UTC to mark an actual moment, represented by the OffsetDateTime clase.

OffsetDateTime odt = OffsetDateTime.of( ldt , offset );


Una zona horaria es una compensación y también rules for handling anomalies such as Daylight Saving Time (DST). So better to use a time zone than a mere offset.

For example, if the context of this data is known to be time in India, use a time zone such as Asia/Kolkata conseguir un ZonedDateTime.

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );

Respondido el 29 de junio de 16 a las 03:06

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