这可能是重复的,但我无法弄清为什么当将MMM指定为MMM并与mm(数字)一起使用时,该月返回零。这里的任何帮助将不胜感激?
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;
public class time1 {
public static void main(String[] args) {
DateFormat originalFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = null;
try {
date = originalFormat.parse("26-Aug-2011");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String formattedDate = targetFormat.format(date);
System.out.println("old date: " + date);
System.out.println("new date: " + formattedDate);
}
}
Run Code Online (Sandbox Code Playgroud)
输出为:
旧日期:IST 2011年8月26日星期五00:00:00
新日期:2011 -00- 26年
当格式更改为dd-mm-yyyy且日期为26-08-2011时,输出为
旧日期:IST 2011年1月26日星期三00:07:00 …
我尝试从毫秒转换为日期字符串。但是,结果并不像我预期的那样正确。
输入是毫秒(例如:1508206600485)
我的时区是 UTC +10:00
------Expected-------------------------------------------- Actual------
01:32 (PM) 17/10/2017--------------------------------02:32 (PM) 17/10/2017
Run Code Online (Sandbox Code Playgroud)
这是方法
public static String getDate(long milliSeconds) {
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM/yyyy");
String dateString = formatter.format(new Date(milliSeconds));
return dateString;
}
Run Code Online (Sandbox Code Playgroud) 我有要求
为了达到这个目的,我编写了以下代码片段,但是没有产生所需的结果.它存储的价值为10/11/2017 4:05,然而,当呈现给美国,即获得价值/刷新页面时,它又 增加了5个小时.删除了异常和其他不必要的代码,使其变得简单:
public class DatetoString implements Serializable
{
private final DateFormat dateFormatter = createDateFormatter();
// Sets Date to model
public void setTypedValue(final Object val)
{
final String dateValue;
String dateTimeFormat = BooleanUtils.isFalse(getUSDateFormatConfig()) ? "dd/MM/yyyy HH:mm" : "MM/dd/yyyy HH:mm";
DateFormat df = new SimpleDateFormat(dateTimeFormat);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date singleDate = (Date) df.parse(val.toString());
dateValue = dateFormatter.format(singleDate);
model.setValue(dateValue.toString());
// Other code..
}
// Retrieves date from model
public Object …Run Code Online (Sandbox Code Playgroud) 我正在尝试格式化具有date以下格式的键的JSON :
日期:“ 2017-05-23T06:25:50”
我当前的尝试如下:
private String formatDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");
String formattedDate = "";
try {
formattedDate = format.format(format.parse(String.valueOf(date)));
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,结果不会显示在我的应用中,它TextView是不可见的,并且我无法记录任何内容。
我确实尝试过其他解决方案,但对我而言没有成功。我不知道是否是因为传入的值是一个字符串。
我正在使用 Kotlin 构建一个简单的年龄计算器应用程序。
public fun calculateage(view: View){
val dob: String = etbirth.text.toString()
val currentyear = calendar.getInstance().get(Calendar.Year)
val age = currentyear - dob.toInt()
tvage.setText("$age")
}
Run Code Online (Sandbox Code Playgroud)
此方法在 API 级别 15 中不可用,至少需要 API 级别 24。
那么如何获取当前年份呢?
基于如何在Java中将标准日期转换为分钟?信息,我按照这种技术将日期转换为分钟,但输出不是预期值(例如,12小时 - >应该是720分钟).
这是我的源代码
SimpleDateFormat sdf = new SimpleDateFormat("hh");
Date theDate = sdf.parse ("12");
long minutes22 = (theDate.getTime() / 1000) / 60;
System.out.println("Minute "+minutes22);
Run Code Online (Sandbox Code Playgroud)
输出为-420.有人可以向我解释一下吗?
<第一个问题解决了,请看评论部分>
新问题是切割String.<问题也解决了,请看评论部分>
对于另一个我尝试做自己的解决方案的输入,但在完成切割字符串后出现问题.我发现重复值不能单独保存.我不知道为什么会发生,因为我保持不同的索引.有什么建议吗?
这是另一个我的另一个源代码.
String date = "Sun 10:00-20:00\r\n" +
"Fri 05:00-10:00\r\n" +
"Fri 16:30-23:50\r\n" +
"Sat 10:00-24:00\r\n" +
"Sun 01:00-04:00\r\n" +
"Sat 02:00-06:00\r\n" +
"Tue 03:30-18:15\r\n" +
"Tue 19:00-20:00\r\n" +
"Wed 04:25-15:14\r\n" +
"Wed 15:14-22:40\r\n" +
"Thu 00:00-23:59\r\n" +
"Mon 05:00-13:00\r\n" +
"Mon 15:00-21:00";
List<String> Hour = new ArrayList<String>();
int loop = …Run Code Online (Sandbox Code Playgroud) 我已经为此工作了几个小时,但我似乎无法弄清楚如何将日期保存到我的 Room Sqllite 数据库中。我基本上是从 Android 文档中复制代码来进行批量处理。
这就是我所拥有的。
数据库:
@Database(entities = {Review.class},
version = 3,
exportSchema=false)
@TypeConverters(DateTypeConverter.class)
public abstract class NotecardDatabase extends RoomDatabase {
...etc...
}
Run Code Online (Sandbox Code Playgroud)
实体:
@Entity(tableName = "review",
indices = {
@Index(value = "next_review"),
}
public class Review {
...Other columns...
@TypeConverters(DateTypeConverter.class)
@ColumnInfo(name ="next_review")
@NonNull
private Date nextReview;
}
Run Code Online (Sandbox Code Playgroud)
接下来,我的转换器:
public class DateTypeConverter {
private static Logger log = Logger.getLogger("DateTypeConverter");
@TypeConverter
public static Date fromTimestamp(Long value) {
if(value != null) {
log.info("Incoming long: " + value + "\nTo Date: …Run Code Online (Sandbox Code Playgroud) 在我的 Spring Boot 应用程序中,我需要处理带有日期时间字段的表单并将其转换为LocalDateTimeJava 格式。
我指定了模式"YYYY-MM-dd HH:mm",但当我提交带有输入值的表单时,它无法转换1990-01-01 10:10。
这是表单对象:
public class UserForm {
@NotNull
@DateTimeFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime dateTime;
// getters, setters
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RequestMapping("/users")
@Controller
public class UserController {
@GetMapping("")
public String userForm(UserForm userForm) {
return "/users/form";
}
@PostMapping("")
public String postForm(@Valid UserForm userForm, BindingResult bindingResult) {
System.out.println(userForm + " " + bindingResult);
return "/users/form";
}
}
Run Code Online (Sandbox Code Playgroud)
和百里香形式:
<form th:object="${userForm}" th:action="@{/users}" method="post">
<span th:each="err: ${#fields.errors('dateTime')}" th:text="${err}" style="background-color:red;color:white;"/>
<input type="text" th:field="*{dateTime}"/>
<input …Run Code Online (Sandbox Code Playgroud) 在我的 Java Spring MVC Web Application,我正在尝试编写一个程序来保存一些带有时间戳的数据。但是我有一个我必须排除的特定时间段。
例如,我有以下时间戳: Sat Jul 28 17:03:00 IST 2018这是一个 Java 日期对象。在保存到数据库之前,我必须检查这个时间是否在指定范围内,如果我必须在时间范围结束后获得下一分钟。
但时间范围是以下格式的字符串:
String startTime = "5:03 PM";
String endTime = "5:10 PM";
Run Code Online (Sandbox Code Playgroud)
因此,如果我的 timeStamp 介于这段时间之间,则我的时间戳应更新为Sat Jul 28 17:11:00 IST 2018.
我现在正在寻找比较两个时间值的方法,因为一个是字符串,另一个是时间戳。
有什么办法可以转换吗String endTime = "5:10 PM"?到Sat Jul 28 17:11:00 IST 2018或只是比较的部分时间,计算出分的区别,并添加到时间戳。
java 6.0
假设今天是: 2018年8月8日
这里方法返回2个日期的差异,以月为单位.
public static Integer getDiffMonths(Date dateFrom, Date dateTo) {
Calendar cal1 = new GregorianCalendar();
cal1.setTime(dateFrom);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(dateTo);
return differenceInMonths(cal1, cal2);
}
private static Integer differenceInMonths(Calendar dateFrom, Calendar dateTo) {
int month1 = dateFrom.get(Calendar.YEAR) * 12 + dateFrom.get(Calendar.MONTH);
int month2 = dateTo.get(Calendar.YEAR) * 12 + dateTo.get(Calendar.MONTH);
int diff = month2 - month1;
return diff;
}
Run Code Online (Sandbox Code Playgroud)
这里有4个测试用例的结果:
1.
currentDate = Aug 08 2018
DateTo = Aug 13 2018
diffMonth = 0
Run Code Online (Sandbox Code Playgroud)
2.
currentDate = …Run Code Online (Sandbox Code Playgroud) java ×10
android ×3
date ×3
datetime ×2
java-date ×2
android-room ×1
calendar ×1
date-format ×1
duration ×1
jodatime ×1
kotlin ×1
minute ×1
spring ×1
spring-boot ×1
sqlite ×1
string ×1
validation ×1