-6

I am getting time and date in this format 2012-03-14 12:33:30.000 from server. I want to remove the time part and keep just the date part like this 2012-03-14.

I get all outputs in this format and would like to know how I can remove the time part for all. Any help will be appreciated.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
CoderJay
  • 1
  • 6

4 Answers4

2

If you want to play with Java time, you could parse the source with the relevant format, then format it back to fit your needs :

String str = "2012-03-14 12:33:30.000";

DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

LocalDateTime dateTime = LocalDateTime.parse(str, sourceFormatter);

System.out.println(targetFormatter.format(dateTime));
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 2
    I agree that this is the best and nicest approach. Still better, use `DateTimeFormatter.ISO_LOCAL_DATE` as your second formatter. The format has already been defined for us, so no need to reinvent the wheel here. – Ole V.V. Jul 10 '18 at 12:27
  • `java.time.format.DateTimeFormatter#ofPattern` requires API level 26 Oreo, which is only 5.7% of devices. – Yousuf Sohail Jul 10 '18 at 12:36
  • 1
    @YousufSohail It sounds like you are talking about Android? (1) I saw nothing in the question mentioning Android. (2) If you want to use this answer on not-brand-new Android, get the ThreeTenABP library, and it will work fine. See [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jul 10 '18 at 12:38
0

One of the ways of achieving this is like this

str = str.substring(10);

Another way is like this

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(yourString);//yourString is your String variable
date.toString();//or whatever
theanilpaudel
  • 3,348
  • 8
  • 39
  • 67
  • 2
    You mean `str = str.substring(0,10);` *I want to remove the time part and keep just the date part*. Also if you're recommending a date/time class, consider [LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) and not `Date`. – achAmháin Jul 10 '18 at 12:15
  • you can format it like that also, check the updated answer – theanilpaudel Jul 10 '18 at 12:16
  • 2
    Please do not recommend the usage of `SimpleDateFormat` which is horribly outdated. Use `DateTimeFormatter` instead. – Ben Jul 10 '18 at 12:19
  • I agree that using standard date-time classes for the task is a good idea. However please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jul 10 '18 at 12:24
0

Using the split method, for example:

if you having the date = 2012-03-14 12:33:30.000 you can do

String text[] = date.split(' ') - (with a space in between
 '') and get de text[0] 
-2

First parse your date string to Java Date object which will contain both date and time, then get the date from that Date object.

Yousuf Sohail
  • 579
  • 5
  • 19
  • A good idea, but vaguely described. Some of the other answers are somewhat more precise about the same. – Ole V.V. Jul 10 '18 at 12:25