9

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

Android Killer
  • 18,174
  • 13
  • 67
  • 90

5 Answers5

10

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

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • you are right may be. Thanks for your answer. I will try it if i will face any situation similarly in future. :) – Android Killer Oct 19 '14 at 16:32
  • 1
    Ok. The answer you marked as correct, though, seems to be wrong (the extra backslash is not needed before the n "\\n"). – Suragch Oct 20 '14 at 04:15
2

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.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
2

If you're displaying to an HTML view, you will want to make sure you use an HTML line break <br/>, instead of \n.

mprivat
  • 21,582
  • 4
  • 54
  • 64
2

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";
GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
  • i need different time always cant use ur hard-coded value and also i tried using 2 back slashes – Android Killer Sep 06 '12 at 15:09
  • simple '\n' works.May be there was some problem with my JSON i am storing this into.My first way started working correctly after i took the same code inside another class.Thanks anyway.. – Android Killer Sep 09 '12 at 15:54
1

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?

Ridcully
  • 23,362
  • 7
  • 71
  • 86