1507:转变日期格式
This commit is contained in:
parent
90c8282cd6
commit
0fc920dbe0
70
src/main/java/leetcode/editor/cn/ReformatDate.java
Normal file
70
src/main/java/leetcode/editor/cn/ReformatDate.java
Normal file
@ -0,0 +1,70 @@
|
||||
//<p>给你一个字符串 <code>date</code> ,它的格式为 <code>Day Month Year</code> ,其中:</p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>Day</code> 是集合 <code>{"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}</code> 中的一个元素。</li>
|
||||
// <li><code>Month</code> 是集合 <code>{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}</code> 中的一个元素。</li>
|
||||
// <li><code>Year</code> 的范围在 <code>[1900, 2100]</code> 之间。</li>
|
||||
//</ul>
|
||||
//
|
||||
//<p>请你将字符串转变为 <code>YYYY-MM-DD</code> 的格式,其中:</p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>YYYY</code> 表示 4 位的年份。</li>
|
||||
// <li><code>MM</code> 表示 2 位的月份。</li>
|
||||
// <li><code>DD</code> 表示 2 位的天数。</li>
|
||||
//</ul>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>示例 1:</strong></p>
|
||||
//
|
||||
//<pre><strong>输入:</strong>date = "20th Oct 2052"
|
||||
//<strong>输出:</strong>"2052-10-20"
|
||||
//</pre>
|
||||
//
|
||||
//<p><strong>示例 2:</strong></p>
|
||||
//
|
||||
//<pre><strong>输入:</strong>date = "6th Jun 1933"
|
||||
//<strong>输出:</strong>"1933-06-06"
|
||||
//</pre>
|
||||
//
|
||||
//<p><strong>示例 3:</strong></p>
|
||||
//
|
||||
//<pre><strong>输入:</strong>date = "26th May 1960"
|
||||
//<strong>输出:</strong>"1960-05-26"
|
||||
//</pre>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li>给定日期保证是合法的,所以不需要处理异常输入。</li>
|
||||
//</ul>
|
||||
//<div><div>Related Topics</div><div><li>字符串</li></div></div><br><div><li>👍 15</li><li>👎 0</li></div>
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
// 1507:转变日期格式
|
||||
public class ReformatDate {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new ReformatDate().new Solution();
|
||||
// TO TEST
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String reformatDate(String date) {
|
||||
String[] strs = date.split(" ");
|
||||
List<String> months = Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
|
||||
int day = Integer.parseInt(strs[0].substring(0, strs[0].length() - 2));
|
||||
int month = months.indexOf(strs[1]) + 1;
|
||||
int year = Integer.parseInt(strs[2]);
|
||||
return String.format("%04d-%02d-%02d", year, month, day);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
44
src/main/java/leetcode/editor/cn/doc/content/ReformatDate.md
Normal file
44
src/main/java/leetcode/editor/cn/doc/content/ReformatDate.md
Normal file
@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串 <code>date</code> ,它的格式为 <code>Day Month Year</code> ,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Day</code> 是集合 <code>{"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}</code> 中的一个元素。</li>
|
||||
<li><code>Month</code> 是集合 <code>{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}</code> 中的一个元素。</li>
|
||||
<li><code>Year</code> 的范围在 <code>[1900, 2100]</code> 之间。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你将字符串转变为 <code>YYYY-MM-DD</code> 的格式,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>YYYY</code> 表示 4 位的年份。</li>
|
||||
<li><code>MM</code> 表示 2 位的月份。</li>
|
||||
<li><code>DD</code> 表示 2 位的天数。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>date = "20th Oct 2052"
|
||||
<strong>输出:</strong>"2052-10-20"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>date = "6th Jun 1933"
|
||||
<strong>输出:</strong>"1933-06-06"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>date = "26th May 1960"
|
||||
<strong>输出:</strong>"1960-05-26"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给定日期保证是合法的,所以不需要处理异常输入。</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>字符串</li></div></div><br><div><li>👍 15</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user