Java中的DateFormat用法举例

@DateTimeFormat 和 @JsonFormat 注解详解 这一篇文章足以让你对Java当中Date时间上的理解更上一层楼,本篇文章主要通过代码的形式来进行试验,彻彻底底搞明白日期传参,以及日期返回参数的格式相关问题,每一个步骤都会记得特别详细!@DateTimeFormat究竟在什么时候用?@JsonFormat在什么时候用?二者有什么区别?同时也会重点围绕这几个问题讲解! 阅读详情
import java.util.Date;  
import java.text.DateFormat;  
  
/** 
 * 格式化时间类 DateFormat.FULL = 0 DateFormat.DEFAULT = 2 DateFormat.LONG = 1 
 * DateFormat.MEDIUM = 2 DateFormat.SHORT = 3 
 */  
  
public class Test {  
    public static void main(String[] args) {  
        Date d = new Date();  
        String s;  
  
        /** Date类的格式: Sat Apr 16 13:17:29 CST 2006 */  
        System.out.println(d);  
  
        System.out.println("******************************************");  
  
        /** getDateInstance() */  
        /** 输出格式: 2006-4-16 */  
        s = DateFormat.getDateInstance().format(d);  
        System.out.println(s);  
  
        /** 输出格式: 2006-4-16 */  
        s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);  
        System.out.println(s);  
  
        /** 输出格式: 2006年4月16日 星期六 */  
        s = DateFormat.getDateInstance(DateFormat.FULL).format(d);  
        System.out.println(s);  
  
        /** 输出格式: 2006-4-16 */  
        s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);  
        System.out.println(s);  
  
        /** 输出格式: 06-4-16 */  
        s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);  
        System.out.println(s);  
  
        /** 输出格式: 2006-01-01 00:00:00 */  
        java.text.DateFormat format1 = new java.text.SimpleDateFormat(  
                "yyyy-MM-dd hh:mm:ss");  
        s = format1.format(new Date());  
        System.out.println(s);  
  
        /** 输出格式: 2006-01-01 01:00:00 */  
        System.out.println((new java.text.SimpleDateFormat(  
                "yyyy-MM-dd hh:mm:ss")).format(new Date()));  
  
        /** 输出格式: 2006-01-01 13:00:00 */  
        System.out.println((new java.text.SimpleDateFormat(  
                "yyyy-MM-dd HH:mm:ss")).format(new Date()));  
  
        /** 输出格式: 20060101000000 ***/  
        java.text.DateFormat format2 = new java.text.SimpleDateFormat(  
                "yyyyMMddhhmmss");  
        s = format2.format(new Date());  
        System.out.println(s);  
    }  
}      

补充一下: 

Calendar cal = Calendar.getInstance();  
cal.setTime(new Date());  
System.out.println("System   Date:   " + cal.get(Calendar.MONTH + 1)); 

注意下,月份是从0开始的,要Calendar.MONTH+1才可以的。


【Java后端】@DateTimeFormat @JsonFormat 时间格式传参总结 @DateTimeF最近在时间Date传参时,没有加注解、前端多种格式都可以接收到数据,从而总结一下其中的原因。在Spring Boot 2.x中,当前端通过POST请求发送一个时间字符串"2023-06-20 21:04:50"时,后端使用Date类型来接收,并且没有标注任何注解,仍然可以接收到数据的原因是因为Spring Boot默认使用了Jackson库进行JSON序列化和反序列化。ISO-8601日期格式:2023-06-20日期+时间格式:2023-06-20T21:04:50(必须含有中间的T) 阅读详情

相关推荐

DateFormat类:日期格式化的便捷工具

在软件开发中,处理日期和时间是一个常见而重要的任务。为了满足不同的需求,Java提供了DateFormat类,它是一个日期格式化的便捷工具。本文将介绍DateFormat类的基本用法以及一些常见的应用场景,帮助读者更好地理解和使用这个类。DateFormat类是Java中用于日期和时间格式化的类。它提供了一组方法,可以将日期和时间对象格式化为特定的字符串,或者将字符串解析为日期和时间对象。DateFormat类是抽象类,具体的实现类有SimpleDateFormat等。

pleaseprintf的博客 6762

java dateformat_java-DateFormat

1、DateFormat类概述DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat。2、SimpleDateFormat构造方法public SimpleDateFormat():将一个 Date 格式化为日期/时间字符串。(默认模式)public SimpleDateFormat(String ...

weixin_39928940的博客 3807

[渗透测试实战篇-4] RCE漏洞组合拳突破Getshell全解析

本文深入解析RCE漏洞组合拳在渗透测试中的实战应用,通过SSRF、XXE等漏洞串联技术突破Getshell的全过程。文章详细介绍了Java和PHP环境下的漏洞利用差异,以及防御绕过与权限维持的实用技巧,为安全研究人员提供全面的实战指导。

weixin_29211309的博客 351

详解Java中格式化日期的DateFormat与SimpleDateFormat类

DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat类完成,下面我们具体来看一下两个类的用法: DateFormat1. DateFormat 介绍DateFormat 的作用是 格式化并解析“日期/时间”。实际上,它是Date的格式化工具,它能帮助...

weixin_33877092的博客 1919

Fedproto:原型聚合的联邦学习框架

文中说明,联邦学习通过计算传递梯度聚合模型时,客户端之间的异构性通常会阻碍模型优化收敛和泛化性能。故本文提出新的FedProto框架,将梯度替换成为原型,通过计算、聚合从不同客户端收集到的本地原型,然后将全局原型发送回所有客户端,以规范本地模型的训练。对每个客户机进行局部训练的目的是最小化对本地数据的分类损失,同时保持生成的本地原型足够接近相应的全局原型(损失函数最小+正则)。什么是原型?文中提到:“

weixin_47754029的博客 2649

在微信小程序中如何引入iconfont

略

sayyy的专栏 4326

java中dateFormat是什么_Java中的DateFormat用法举例

觉得这个DateFormat是比较实用,记录一下,文章来自csdn博客中的一个名叫空心兜兜的网友。class="java">import java.util.Date;import java.text.DateFormat;/*** 格式化时间类 DateFormat.FULL = 0 DateFormat.DEFAULT = 2 DateFormat.LONG = 1* DateForma...

weixin_31109967的博客 2206

ADNI数据集下载(详细)

刚刚接触到这个数据集的时候比较头疼,用了好长的时间才把下载过程和预处理过程弄明白。需要发送电子邮件给官方平台,收到回信后才能够下载到数据集。 1、ADNI账号注册 第一步:搜索百度打开ADNI 第二步,点击ADTA&SAMPLES 第三步:一直将鼠标往下拉 点击ADNI,进入里面填写信息。需要填写的信息还是挺多的,耐心填写,再等到邮件回复就可以了 2、ADNI数据集下载 登录申请的邮...

蜗牛遥遥 3万+

java dateformat_Java中的DateFormat用法

importjava.util.Date;importjava.text.DateFormat;/*** 格式化时间类 DateFormat.FULL = 0 DateFormat.DEFAULT = 2 DateFormat.LONG = 1* DateFormat.MEDIUM = 2 DateFormat.SHORT = 3*/public classTest {public static ...

weixin_29147347的博客 2072

【Unity 实用工具篇】| Unity中 实现背景模糊效果,简单易用

背景模糊 是一个很实用的功能,它可以帮助游戏在视觉上有更好的专注力和表现力。 实现模糊的种类和方式有很多种,大家自行选择适合自己的就可以了。 下面一起来看下本文实现背景模糊的效果和使用方式吧!

努力前行,总会成为自己心中的那道光 4万+

RK356x U-Boot研究所(命令篇)3.12 mtd命令的用法

RK356x U-Boot研究所(命令篇)3.12 mtd命令的用法

Systemcall名屋 1686

Java中的日期时间类详解(Date、DateFormat、Calendar)

目录1 Date类1.1 概述1.2 Date类构造方法2 DateFormat类2.1 其子类SimpleDateFormat的构造方法2.2 DateFormat类常用方法2.2.1 format方法2.2.2 parse方法2.3 综合练习3 Calendar类3.1 概念3.2 获取方式3.3 常用方法3.3.1 get/set方法3.3.2 add方法3.3.3 getTime方法 1 Date类 1.1 概述 java.util.Date类表示特定的瞬间,精确到毫秒。Date类的构造函数可以把毫

sc179的博客 4万+

华为OD机试双机位C卷-MVP争夺战 (C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【MVP争夺战】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试 - MVP争夺战,

qq_45776114的博客 1172

java format用法_Java中的DateFormat用法

importjava.util.Date;importjava.text.DateFormat;/*** 格式化时间类 DateFormat.FULL = 0 DateFormat.DEFAULT = 2 DateFormat.LONG = 1* DateFormat.MEDIUM = 2 DateFormat.SHORT = 3*/public classTest {public static ...

weixin_39932947的博客 330

C++(Qt)软件调试---静态分析工具clang-tidy(18)

现在很多人在开发中完全忽略了编译器、IDE的警告提示,这怎么可能写出稳定的程序。 clang-tidy是一个由LLVM项目提供的开源工具,是一个静态分析工具,用于进行静态代码分析和代码质量改进。

mahuifa的博客 5082

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Java中的格式化类——DateFormat类

作用:DateFormat类专门用于将日期格式化为字符串或者将用特定格式显示的日期字符串转换成一个Date对象。 说明:DateFormat是一个抽象类,不能被直接实例化 但它提供了一系列的静态方法来获取DateFormat类的实例对象,并能调用其他相应的方法进行操作。 方法声明 功能描述 static DateFormat getDateInstance() 用于创建默认语言环境和格式化风格的日期格式器 static DateFormat getDateInstan

hey,欢迎浏览我的博客,希望能给你提供帮助。 5870

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

java date dateformat_Java常用类之Date和DateFormat用法

Java常用类之Date和DateFormat用法---201508021.Date时间类(java.util.Date)-->在标准Java类库中包含一个Date类。它的对象表示一个特定的瞬间,精确到毫秒-->Java中时间的表示说白了也是数字,是从:标准纪元1970.1.1 0点开始到某个时刻的毫秒数,类型为long。此类学习之建议----看源码代码: MyFundamental....

weixin_39992760的博客 199

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Java常用类之Date和DateFormat用法

Java常用类之Date和DateFormat用法 ---20150802 1.Date时间类(java.util.Date) -->在标准Java类库中包含一个Date类。它的对象表示一个特定的瞬间,精确到毫秒 -->Java中时间的表示说白了也是数字,是从:标准纪元1970.1.1 0点开始到某个时刻的毫秒数,类型为long。 此类学习之建议----看源码

龙腾四海365的专栏 943

java中Dateformat类的详细使用(详解)_java dateformat

一个好的心态和一个坚持的心很重要,很多冲着高薪的人想学习前端,但是能学到最后的没有几个,遇到困难就放弃了,这种人到处都是,就是因为有的东西难,所以他的回报才很大,我们评判一个前端开发者是什么水平,就是他解决问题的能力有多强。分享一些前端面试题以及学习路线给大家### 最后。

2401_84619287的博客 2530

java中Dateformat类的详细使用(详解)

DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat类完成,下面我们具体来看一下两个类的用法: DateFormat DateFormat 介绍 DateFormat 的作用是 格式化并解析“日期/时间”。实际上,它是Date的格式化工具,它能帮助我...

南 墙 3万+
上一篇: Java代码 swt对话框
下一篇: MyEclipse 工程使用相对路径导入Jar包设置
ucasliming
博客等级 码龄16年 126粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值