- Java 编程示例
- 示例 - Home
- 示例 - 环境
- 示例 - 字符串
- 示例 - 数组
- 示例 - 日期和时间
- 示例 - 方法
- 示例 - 文件
- 示例 - 目录
- 示例 - 异常
- 示例 - 数据结构
- 示例 - 集合
- 示例 - 网络
- 示例 - 线程
- 示例 - 小程序
- 示例 - 简单 GUI
- 示例 - JDBC
- 示例 - 正则表达式
- 示例 - Apache PDFBox
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- Java 有用资源
- Java - 快速指南
- Java - 有用资源
如何使用 Java 以 (MMM) 格式显示月份名称
问题描述
如何以 (MMM) 格式显示月份名称?
解决方案
此示例展示了如何借助 Calender 类的 Calender.getInstance() 方法和 Formatter 类的 fmt.format() 方法,以 (MMM) 格式显示当前月份。
import java.util.Calendar;
import java.util.Formatter;
public class MainClass{
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt = new Formatter();
fmt.format("%tB %tb %tm", cal, cal, cal);
System.out.println(fmt);
}
}
结果
以上代码示例将产生以下结果。
October Oct 10
月份格式的另一个示例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class HelloWorld {
public static void main(String[] args) {
SimpleDateFormat f = new SimpleDateFormat("MMM");
SimpleDateFormat f1 = new SimpleDateFormat("dd");
SimpleDateFormat f2 = new SimpleDateFormat("a");
int h;
if(Calendar.getInstance().get(Calendar.HOUR)== 0)h = 12;
else h = Calendar.getInstance().get(Calendar.HOUR);
String filename="Current Date is :"
+f1.format(new Date())
+f.format(new Date())
+h+f2.format(new Date());
System.out.println(filename);
}
}
以上代码示例将产生以下结果。
Current Date is :11Nov5AM
java_date_time.htm
广告