-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.

6
  • How do you modify a string in java? Start with that and you will find your answer. Commented Jul 10, 2018 at 12:11
  • 1
    str = str.substring(0,10); Commented Jul 10, 2018 at 12:12
  • How do you get this time data, what is your object? Commented Jul 10, 2018 at 12:13
  • this works @notyou Commented Jul 10, 2018 at 12:18
  • That’s a perfectly valid way of looking at it, @Deadpool. As you can see from the answers, there are others too. Commented Jul 10, 2018 at 12:35

4 Answers 4

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));
Sign up to request clarification or add additional context in comments.

3 Comments

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.
java.time.format.DateTimeFormatter#ofPattern requires API level 26 Oreo, which is only 5.7% of devices.
@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.
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

4 Comments

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 and not Date.
you can format it like that also, check the updated answer
Please do not recommend the usage of SimpleDateFormat which is horribly outdated. Use DateTimeFormatter instead.
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 and its DateTimeFormatter.
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] 

3 Comments

What language and library are you using? (The question was tagged java.)
The language is Java and the library is Date
Thanks for replying. java.util.Date hasn’t got methods getFullYear nor getFullMonth (nor split for that matter). Your answer won’t work.
-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.

1 Comment

A good idea, but vaguely described. Some of the other answers are somewhat more precise about the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.