Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create new line between dateformat

Tags:

java

android

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

like image 504
Android Killer Avatar asked Sep 06 '12 12:09

Android Killer


1 Answers

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

like image 83
Suragch Avatar answered Sep 17 '22 20:09

Suragch