Reputation: 18509
i m creating date format like this :
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE, h:mm a");
i need a new line between date, month and time something like this
thus ,sep 6
4:25pm
so i made the following changes :
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,"+"\n"+" h:mm a");
it did not give me anything just it created it in one line like this :
thus ,sep 6 4:25pm
so i took format object like this
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,");
SimpleDateFormat sdf1 =new SimpleDateFormat(" h:mm a");
and did this :
sdf.format(calendar.getTime())+"\n"+sdf1.format(calendar.getTime())
but it again gives the same result
thus ,sep 6 4:25pm
calendar is a Calendar object.Any help will be appreciated!!
Upvotes: 9
Views: 8548
Reputation: 512666
I see from one of your comments that your original solution actually works, but I had the same question when I came here, so let me add an answer to this question. (My formatting is a little different than yours.)
Date date = new Date(unixMilliseconds);
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy\nh:mma");
String formattedDate = sdf.format(date);
The \n
in MMM d, yyyy\nh:mma
works because neither the \
nor the n
are interpreted by SimpleDateFormat
(see documentation) and thus are passed on to the Java String. If they did have special meaning you could have used single quotes: MMM d, yyyy'\n'h:mma
(which also works).
Upvotes: 10
Reputation: 10379
Don't use \n, use :
System.getProperty("line.separator");
to get the line separator.
I found another source which tells that you can use 

to have a carriage return.
Upvotes: 2
Reputation: 8386
I think Android literally needs \n
to appear in the string, not an actual newline character. So, you need to escape the backslash in your Java string, so something like this:
String output = "Thus ,Sep 6" + "\\n" + "4:25pm";
Upvotes: 2
Reputation: 21912
If you're displaying to an HTML view, you will want to make sure you use an HTML line break <br/>
, instead of \n
.
Upvotes: 2
Reputation: 23665
Are you sure, that the \n
disappears? At least your last try cannot have anything to do with the date format. Do you per chance create your output for a web page and should use <br />
instread of \n
?
Upvotes: 1