How to extract date and time from string timestamp in java

I get the date and time both String TIMESTAMPfrom MySQL from the server in this format:

2014-02-15 05:18:08

I want to extract the date in format DD-MM-YYYYand the time in format HH:MM:SS AM/PM. Also the time zone of this timestamp is different, and I want it in the Indian time zone (IST).

Remember that it TIMESTAMPhas a data type String.

+4
source share
5 answers

Use java.text.SimpleDateFormatandjava.util.TimeZone

What time zone is the date string in? Replace time zone UTCwith this time zone.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("2014-02-15 05:18:08");

SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
sdf2.setTimeZone(TimeZone.getTimeZone("IST"));
String dateStr = sdf2.format(date); // Output: 15-02-2014 10:48:08 AM

: (24 /12 ) ? , 24- , AM/PM .

12- , AM/PM, ​​ 2014-02-15 05:18:08 PM. sdf new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")

======================== : =====================

" "...

SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy");
sdfDate.setTimeZone(java.util.TimeZone.getTimeZone("IST"));

SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss a");
sdfTime.setTimeZone(java.util.TimeZone.getTimeZone("IST"));

String dateStr = sdfDate.format(date);
String timeStr = sdfTime.format(date);
+5

Yatendra Goel .

Joda

, Joda-Time 2.3.

, Joda-Time . java.time. . java.time.

FYI... UTC/GMT. , .

String input = "2014-02-15 05:18:08";
input = input.replace( " ", "T" ); // Replace space in middle with a "T" to get ISO 8601 format.

// Parse input as DateTime in UTC/GMT.
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
// Adjust to India time.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTime = dateTimeUtc.withZone( timeZone );

// Using "en" for English here because (a) it is irrelevant in our case, and (b) I don't know any Indian language codes.
java.util.Locale localeIndiaEnglish = new Locale( "en", "IN" ); // ( language code, country code );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "SS" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
String output = formatter.print( dateTime );

DateTimeFormatter formatterDateOnly = DateTimeFormat.forPattern( "dd-MM-yyyy" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
DateTimeFormatter formatterTimeOnly = DateTimeFormat.forPattern( "hh:mm:ss a" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
String dateOnly = formatterDateOnly.print( dateTime );
String timeOnly = formatterTimeOnly.print( dateTime );

...

System.out.println( "input: " + input );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTime: " + dateTime );
System.out.println( "output: " + output );
System.out.println( "dateOnly: " + dateOnly );
System.out.println( "timeOnly: " + timeOnly );

...

input: 2014-02-15T05:18:08
dateTimeUtc: 2014-02-15T05:18:08.000Z
dateTime: 2014-02-15T10:48:08.000+05:30
output: 15/2/14 10:48 AM
dateOnly: 15-02-2014
timeOnly: 10:48:08 AM
+1

DATE_FORMAT (, ).

:

SELECT DATE_FORMAT(timestamp, '%e-%c-%Y') FROM table WHERE...

-edit: : "DD-MM-YYYY".

timestamp - mySQL ( : ).

: DATE_FORMAT

0

SimpleDateFormat, : -

String s = new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(yourTimestamp);

SimpleDateFormat.

0

TL;DR

ZonedDateTime zdt = LocalDateTime.parse( "2014-02-15 05:18:08".replace( " " , "T" ) ).atOffset( ZoneOffset.UTC ).atZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) ) ;
LocalDate ld = zdt.toLocalDate();
LocalTime lt = zdt.toLocalTime();

,

, .

JDBC 4.2 java.time- .

Instant instant = Instant.now() ;  // Capture the current moment in UTC.
myPreparedStatement.setObject( … , instant ) ;

.

Instant instant = myResultSet.getObject( … , Instant.class ) ;

java.time

java.time Java 8 , Joda -Time-.

  • java.sql.Timestamp Instant.
  • java.sql.Date LocalDate.
  • java.sql.Time LocalTime.  -

Parsing

, java.time. , Java. , .

ISO 8601. SPACE T.

String input = "2014-02-15 05:18:08".replace( " " , "T" ) ;

LocalDateTime

LocalDateTime, - offset-from-UTC .

LocalDateTime ldt = LocalDateTime.parse( input ) ;

OffsetDateTime

, String UTC . UTC.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

ZonedDateTime

, , UTC.

atZoneSameInstant , ZonedDateTime , OffsetDateTime. , .

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

LocalDate LocalTime

, Local….

LocalDate ld = zdt.toLocalDate();
LocalTime lt = zdt.toLocalTime();

toString , ISO 8601. , DateTimeFormatter. .

- . Locale , , ..

Locale locale = new Locale( "en" , "IN" ); // English language, India cultural norms.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( locale );
String output = zdt.format( f );

java.time

java.time Java 8 . legacy -, java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time . JDBC, JDBC 4.2 , , java.sql.*.

java.time?

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

0
source

Source: https://habr.com/ru/post/1527077/


All Articles