Java日期时间格式化操作DateUtils 的整理

2025-05-29 0 99

Java日期时间格式化操作DateUtils 的整理

直接上代码,总结了开发过程中经常用到的日期时间格式化操作!

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342
import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.concurrent.TimeUnit;

/**

* ClassName: DateUtils <br/>

* Description:时间操作工具类

*/

public class DateUtils {

private static final String[] UNIT_DESC = new String[]{"天", "小时", "分钟", "秒"};

/**

* 获得当前系统时间,格式为yyyyMMdd

*

* @return 格式化后的时间

*/

public static String currentYYYYMMDD() {

return getStrByDate(new Date(), "yyyyMMdd");

}

/**

* 获得当前系统时间,格式为HHmmss

*

* @return 格式化后的时间

*/

public static String currentHHMMSS() {

return getStrByDate(new Date(), "HHmmss");

}

/**

* 获得当前系统时间,格式为yyyyMMddHHmmss

*

* @return 格式化后的时间

*/

public static String currentYYYYMMDDHHmmss() {

return getStrByDate(new Date(), "yyyyMMddHHmmss");

}

/**

* 根据给定的字符串如:yyyy-MM-dd HH:mm:ss,(必须是这种格式) 返回一个日期日期形式

*

* @param strDate 要抛析的字符串,且字符串的形式必须:2007-09-10 07:00:00

* @return 将字符串抛析成日期的格式返回

* @throws ParseException 解析 format 字段失败

*/

public static java.util.Date getDateByStr(String strDate, String format) throws ParseException {

assert strDate != null && format != null;

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

return simpleDateFormat.parse(strDate);

}

/**

* 根据给定的日期,返回给定的字符串, 返回 字符串的形式是:yyyy-MM-dd HH:mm:ss

*

* @param date 要格式化的日期

* @return 将日期格式化后返回的字符串,以这中格式返回:yyyy-MM-dd HH:mm:ss

*/

public static String getStrByDate(Date date, String format) {

assert date != null && format != null;

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

return simpleDateFormat.format(date);

}

/**

* 得到当前时间

*

* @return 当前时间

*/

public static Date getDayOfMonth() {

Calendar now = Calendar.getInstance();

return now.getTime();

}

/**

* 得到每月第一天

*

* @param date 日期

* @return 日期月份的第一天

*/

public static Date getFirstDayOfMonth(Date date) {

Calendar nowday = Calendar.getInstance();

nowday.setTime(date);

nowday.set(Calendar.DATE, 1);// 把日期设置为当月第一天

return nowday.getTime();

}

/**

* 得到每月最后一天

*

* @param date 日期

* @return 日期月份最后一天

*/

public static Date getLastDayOfMonth(Date date) {

Calendar nowday = Calendar.getInstance();

nowday.setTime(date);

nowday.set(Calendar.DATE, 1);// 把日期设置为当月第一天

nowday.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天

return nowday.getTime();

}

/**

* 获取当前年份 格式:yyyy

*

* @param date 当前时间

* @return year

*/

public static String getCurrYear(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");

Date currYear = calendar.getTime();

return String.valueOf(dateFormat.format(currYear));

}

/**

* 获取当前月份 格式:MM

*

* @param date 当前时间

* @return Date

*/

public static String getCurrMonth(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

SimpleDateFormat dateFormat = new SimpleDateFormat("MM");

Date currMonth = calendar.getTime();

return String.valueOf(dateFormat.format(currMonth));

}

/**

* 得到此日期的最后一天

*

* @param d 日期

* @return 最后一天

*/

public static Date getLastDayByDate(Date d) {

Calendar newday = Calendar.getInstance();

newday.setTime(d);

int lastday;

int month = newday.get(Calendar.MONTH);

do {

lastday = newday.get(Calendar.DAY_OF_MONTH);

newday.add(Calendar.DAY_OF_MONTH, 1);

} while (newday.get(Calendar.MONTH) == month);

newday.set(Calendar.MONTH, month);

newday.set(Calendar.DAY_OF_MONTH, lastday);

return newday.getTime();

}

/**

* 将 yyyyMMdd 的字符窜 转化成 yyyy-MM-dd

*

* @param dateString yyyyMMdd格式的日期

* @return yyyy-MM-dd格式的日期

* @throws ParseException

*/

public static String formatyyyyMMdd(String dateString) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

Date date = simpleDateFormat.parse(dateString);

SimpleDateFormat formatStr = new SimpleDateFormat("yyyy-MM-dd");

return formatStr.format(date);

}

/**

* 将 yyyyMMdd 的字符窜 转化成 yyyy-MM-dd HH:mm:ss

*

* @param dateString

* @return

* @throws ParseException

*/

public static String formatyyyyMMddHHmmss(String dateString) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

Date date = simpleDateFormat.parse(dateString);

SimpleDateFormat formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return formatStr.format(date);

}

/**

* 获取当前年份 格式:yyyy

*

* @return Date

*/

public static int getCurrYear() {

Calendar calendar = Calendar.getInstance();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");

Date currYearFirst = calendar.getTime();

return Integer.valueOf(dateFormat.format(currYearFirst));

}

/**

* 获取当前时间前三月

*

* @return Date

*/

public static Date getLastThreeMonths() {

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, -3);

calendar.add(Calendar.DAY_OF_MONTH, 1);

return calendar.getTime();

}

/**

* 获取当前时间前一个月

*

* @return Date

*/

public static Date getLastOneMonths() {

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, -1);

calendar.add(Calendar.DAY_OF_MONTH, 1);

return calendar.getTime();

}

/**

* 获取当前时间前六个月

*

* @return Date

*/

public static Date getLastSixMonths() {

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, -6);

calendar.add(Calendar.DAY_OF_MONTH, 1);

return calendar.getTime();

}

/**

* 获取某年第一天日期

*

* @param year 年份

* @return Date

*/

public static Date getCurrYearFirst(int year) {

Calendar calendar = Calendar.getInstance();

calendar.clear();

calendar.set(Calendar.YEAR, year);

return calendar.getTime();

}

/**

* 获取某年最后一天日期

*

* @param year 年份

* @return Date

*/

public static Date getCurrYearLast(int year) {

Calendar calendar = Calendar.getInstance();

calendar.clear();

calendar.set(Calendar.YEAR, year);

calendar.roll(Calendar.DAY_OF_YEAR, -1);

return calendar.getTime();

}

/**

* 格式化时间

*

* @param date 时间

* @param format 格式化模板

* @return 格式化后的时间

*/

public static String date2Str(Date date, String format) {

return getStrByDate(date, format);

}

/**

* 获得指定日期的前一天 yyyy-MM-dd

* @param date

* @return

*/

public static String getSpecifiedDayBefore(Date date, String dateFormat){

if (date == null) return null;

Calendar c = Calendar.getInstance();

c.setTime(date);

int day=c.get(Calendar.DATE);

c.set(Calendar.DATE,day-1);

String dayBefore=new SimpleDateFormat(dateFormat).format(c.getTime());

return dayBefore;

}

/**

* 获得指定日期的后一天 yyyy-MM-dd

*

* @param date

* @return

*/

public static String getSpecifiedDayAfter(Date date, String dateFormat) {

if (date == null) return null;

Calendar c = Calendar.getInstance();

c.setTime(date);

int day = c.get(Calendar.DATE);

c.set(Calendar.DATE, day + 1);

String dayAfter = new SimpleDateFormat(dateFormat).format(c.getTime());

return dayAfter;

}

/**

* 格式化持续时间<br/>

* 将持续时间,格式化为 xx天xx小时xx分钟xx秒 如果 "xx" 为0 自动缺省。

*

* @param seconds 持续时间,单位(秒)

* @return 格式化后的字符串

* @see TimeUnit 时间单位转换工具

* @since 1.5

*/

public static String convertSeconds2Str(long seconds) {

StringBuilder sb = new StringBuilder();

long[] date = {TimeUnit.SECONDS.toDays(seconds), TimeUnit.SECONDS.toHours(seconds) % 24, TimeUnit.SECONDS.toMinutes(seconds) % 60, TimeUnit.SECONDS.toSeconds(seconds) % 60};

for (int i = 0; i < date.length; i++) {

long l = date[i];

if (l > 0) sb.append(l).append(UNIT_DESC[i]);

}

return sb.toString();

}

/**

* 格式化持续时间

* 将持续时间,格式化为 xx天xx小时xx分钟xx秒 如果 "xx" 为0 自动缺省。

*

* @param seconds 持续时间,单位(分钟)

* @return 格式化后的字符串

* @see TimeUnit 时间单位转换工具

* @since 1.5

*/

public static String convertMinute2Str(long minute) {

StringBuilder sb = new StringBuilder();

long[] date = {TimeUnit.SECONDS.toHours(minute) % 24,TimeUnit.SECONDS.toMinutes(minute) % 60, TimeUnit.SECONDS.toSeconds(minute) % 60};

for (int i = 0; i < date.length; i++) {

long l = date[i];

if (l > 0) sb.append(l).append(UNIT_DESC[i]);

}

return sb.toString();

}

}

以上就是关于java 日期格式化操作的所有内容,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.ibloger.net/article/628.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java日期时间格式化操作DateUtils 的整理 https://www.kuaiidc.com/115769.html

相关文章

发表评论
暂无评论