我正在创建这样的日期格式:
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE, h:mm a");
我需要像这样在日期、月份和时间之间换行
thus ,sep 6
4:25pm
所以我做了以下更改:
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,"+"\n"+" h:mm a");
它没有给我任何东西,只是它像这样在一行中创建了它:
thus ,sep 6 4:25pm
所以我采用了这样的格式对象
SimpleDateFormat sdf =new SimpleDateFormat("MMM d, EEE,");
SimpleDateFormat sdf1 =new SimpleDateFormat(" h:mm a");
并这样做了:
sdf.format(calendar.getTime())+"\n"+sdf1.format(calendar.getTime())
但是还是一样的结果
thus ,sep 6 4:25pm
calendar 是一个 Calendar 对象。我们将不胜感激!!
最佳答案
我从您的一条评论中看到,您原来的解决方案确实有效,但我来这里时也有同样的问题,所以让我来补充一下这个问题的答案。 (我的格式与您的略有不同。)
Date date = new Date(unixMilliseconds);
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy\nh:mma");
String formattedDate = sdf.format(date);
MMM d, yyyy\nh:mma
中的 \n
有效,因为 \
和 n
由 SimpleDateFormat
( see documentation ) 解释,因此传递给 Java 字符串。如果它们确实有特殊含义,您可以使用单引号:MMM d, yyyy'\n'h:mma
(也有效)。
关于java - 如何在日期格式之间创建新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12300571/