How to create a new line between dateformat

im create the date format as follows:

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 

therefore, I made the following changes:

 SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,"+"\n"+" h:mm a"); 

he gave me nothing, only he created it in one line as follows:

 thus ,sep 6 4:25pm 

so I took a format object like this

 SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,"); SimpleDateFormat sdf1 =new SimpleDateFormat(" h:mm a"); 

and did the following:

 sdf.format(calendar.getTime())+"\n"+sdf1.format(calendar.getTime()) 

but he again gives the same result

 thus ,sep 6 4:25pm 

A calendar is a Calendar object. Any help would be appreciated!

+6
source share
5 answers

I think Android literally needs \n to display in a line, not for a newline character. So you need to avoid the backslash in your Java string, so something like this:

 String output = "Thus ,Sep 6" + "\\n" + "4:25pm"; 
+2
source

I see from one of your comments that your original solution really works, but I had the same question when I came here, so let me add an answer to this question. (My formatting is slightly different from yours.)

 Date date = new Date(unixMilliseconds); SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy\nh:mma"); String formattedDate = sdf.format(date); 

\n in MMM d, yyyy\nh:mma works because neither \ nor n are interpreted by SimpleDateFormat ( see the documentation ) and thus are passed to the Java string. If they had a special meaning, you could use single quotes: MMM d, yyyy'\n'h:mma (which also works).

+6
source

If you are showing an HTML view, you need to make sure that you use HTML line break <br/> instead of \n .

+2
source

Are you sure \n disappearing? At least your last attempt may not have anything to do with the date format. Do you accidentally create your output for a web page and have to use <br /> instread from \n ?

+1
source

Do not use \ n, use:

 System.getProperty("line.separator"); 

to get the line separator.

I found another source that says you can use &#xA; to return the carriage.

+1
source

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


All Articles