小编Sug*_*lai的帖子

获取枚举值作为Java 8中的String列表

是否有任何Java 8方法或简单方法,它将Enum值作为String列表返回,如:

List<String> sEnum = getEnumValuesAsString();
Run Code Online (Sandbox Code Playgroud)

java enums java-8

45
推荐指数
2
解决办法
7万
查看次数

用于解析目录和文件名的正则表达式

我正在尝试编写一个正则表达式,它将使用匹配组解析完全限定路径的目录和文件名.

所以...

/var/log/xyz/10032008.log
Run Code Online (Sandbox Code Playgroud)

将承认group 1 to be "/var/log/xyz"group 2 to be "10032008.log"

看似简单,但我不能让匹配的团队为我的生活工作.

注意:正如一些受访者所指出的,这可能不是正常表达的好用.通常我更喜欢使用我正在使用的语言的文件API.我实际上要做的事情比这复杂得多,但要解释起来要困难得多,所以我选择了一个每个人都熟悉的域名,以便最简洁地描述根本问题.

regex parsing

26
推荐指数
5
解决办法
12万
查看次数

Spring ClassPathResource如何工作?

我在该位置有档案

--src
  --> main
   --> config
    --> application
     --> context
      --> reference
       --> user
        --> user.xml
Run Code Online (Sandbox Code Playgroud)

哪里

    --src
      --> main
       --> config
Run Code Online (Sandbox Code Playgroud)

在类路径中.现在我正在尝试使用

Resource resource = new ClassPathResource("classpath**:/application/context/references/user/user.xml");
File file = resource.getFile();
Run Code Online (Sandbox Code Playgroud)

但我得到了FileNotFoundException,我试过了

Resource resource = new ClassPathResource("classpath:/application/context/references/user/user.xml");
File file = resource.getFile();
Run Code Online (Sandbox Code Playgroud)

也是,但我仍然得到例外.有人能帮我理解ClassPathResource正确的解决方案吗?

java spring

11
推荐指数
1
解决办法
4万
查看次数

日期和时间转换为java中的其他时区

我已编写此代码以将当前系统日期和时间转换为其他时区.我没有收到任何错误,但我没有按预期得到我的输出.就像我在特定时间执行我的程序..我的输出是::

印度当前时间是::Fri Feb 24 16:09:23 IST 2012

:: Central Standard Time中的日期和时间::Sat Feb 25 03:39:23 IST 2012

根据CST时区的实际时间是::

Friday, 24 February 4:39:16 a.m(GMT - 6:00)
Run Code Online (Sandbox Code Playgroud)

所以有一些时间差距.我不知道为什么会这样.任何帮助将不胜感激..代码是::

package MyPackage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Temp2 {


    public static void main(String[] args) {

        try {
            Calendar currentdate = Calendar.getInstance();
            String strdate = null;
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            strdate = formatter.format(currentdate.getTime());
            TimeZone obj = TimeZone.getTimeZone("CST"); …
Run Code Online (Sandbox Code Playgroud)

java timezone date date-conversion

9
推荐指数
3
解决办法
9万
查看次数

Joda LocalDate To LocalDateTime

我正在尝试转换Joda LocalDateJoda LocalDateTime,因为我正在使用该方法toLocalDateTime(LocalTime.MIDNIGHT),因为现在它工作正常例如:对于给定的joda Localdate 2025-02-28我得到预期的joda LocalDateTime 2025-02-28T00:00:00.000,但我担心的是,这种方法是否适用于所有情况.例如during dayLight saving time zone anomalies..etc ..

更新:我对这个问题做了一个小小的研究,就在这里

toLocalDateTime(LocalTime time) 文档说明:将LocalDate对象转换为LocalDateTime LocalTime以填充缺少的字段.

当我初始化LocalTimeLocalTime.MIDNIGHT,从这里开始 LocalTime.MIDNIGHT是一个静态的最终字段new LocalTime(0, 0, 0, 0);,你可以看到它是一个时间值硬编码到零值ISOChronology getInstanceUTC(),所以我想我会得到所需的输出没有任何问题.

java jodatime

9
推荐指数
1
解决办法
1万
查看次数

使用具有超类型属性的类型创建对象表

我正在尝试创建一个oracle-object类型的oracle表.

这是我的对象结构的样子

CREATE OR REPLACE TYPE PERS_T AS OBJECT 
( 

 empno number(4)
, ename varchar2(10)
, job varchar2(9)
, hiredate DATE
, sal number(7,2)
, comm number(7,2)
, deptno number(2)
)NOT FINAL;

CREATE OR REPLACE TYPE EMP_T FORCE UNDER pers_t (
  mgr pers_t
);
Run Code Online (Sandbox Code Playgroud)

所有这些都很好,但当我尝试使用时创建一个EMP_T类型表

CREATE TABLE table_name(emp_type EMP_T);
Run Code Online (Sandbox Code Playgroud)

我收到了错误

SQL Error: ORA-30756: cannot create column or table of type that contains a supertype attribute
Run Code Online (Sandbox Code Playgroud)

在oracle中可以创建这样的表吗?

oracle

9
推荐指数
1
解决办法
713
查看次数

Mock()vs Spy()vs Stub()之间的Spock差异

虽然这个问题已经得到解答,但我还不清楚在嘲笑过程中我应该使用哪一个

虽然指的是spock.lang.MockingApi.java.我无法发现任何这些之间的任何差异.

文档Mock是说

Person person = Mock() // type is Person.class, name is "person"
Run Code Online (Sandbox Code Playgroud)

文档Spy是说

Person person = Spy() // type is Person.class, name is "person"
Run Code Online (Sandbox Code Playgroud)

文档Stub是说

Person person = Stub() // type is Person.class, name is "person"
Run Code Online (Sandbox Code Playgroud)

这清楚地说明这些之间没有任何区别.那么为什么我们有这三种嘲讽策略以及当时和何时使用它们之间的区别.

如果它是示例代码的答案,那将会非常有用.

java unit-testing spock

8
推荐指数
1
解决办法
4726
查看次数

spock - 模拟静态方法不起作用

我正在尝试readAttributes使用 groovy 的metaClass约定来模拟静态方法之一,但实际方法被调用。

这就是我嘲笑静态函数的方式:

def "test"() {
    File file = fold.newFile('file.txt')
    Files.metaClass.static.readAttributes = { path, cls ->
        null
    }

    when:
        fileUtil.fileCreationTime(file)
    then:
        1 * fileUtil.LOG.debug('null attribute')
}
Run Code Online (Sandbox Code Playgroud)

我在这里做错了吗?

我的java方法

public Object fileCreationTime(File file) {
    try {
        BasicFileAttributes attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        if(attributes == null) {
            LOG.debug("null attribute");
        }  
        //doSomething
    } catch (IOException exception) {
        //doSomething
    }
    return new Object();
}
Run Code Online (Sandbox Code Playgroud)

java groovy unit-testing spock

7
推荐指数
2
解决办法
2万
查看次数

EC2MetadataUtils 的替代品

在我们的代码中,为了获取我们正在使用的 ec2 实例的区域EC2MetadataUtils.getEC2InstanceRegion(),我们刚刚意识到我们不能使用它EC2MetadataUtils,因为它是一个内部 API,可能会发生变化。

注意:这是一个内部 API,可能会发生变化。SDK 用户不应依赖于此。

进行了一些谷歌搜索,但无法找到替代解决方案,是否有任何替代解决方案可用于获取 ec2 实例的区域?

谢谢你的帮助!

java spring amazon-ec2 amazon-web-services aws-sdk

7
推荐指数
1
解决办法
617
查看次数

grails spock测试失败'java.lang.IllegalArgumentException:ServletContext不能为null'

我有一个方法command class,使用messageSource.getMessage(...),因为messageSource将不会被注入到commandClass.我用

def messageSource = Holders.applicationContext.getBean("messageSource")在里面commandClass.

我的问题是在尝试编写unit test此方法时,

@Before
void setup() {
    Holders.applicationContext.getBean("messageSource")
}

void "testFunction"() {
    //inside testFunction I am using messageSource
    given:
        //required things
    when:
        //call the function
    then:
        //assert
}
Run Code Online (Sandbox Code Playgroud)

在测试此功能后,我收到错误

java.lang.IllegalArgumentException: ServletContext must not be null at grails.util.Holders.getApplicationContext(Holders.java:80)

有人可以建议如何解决这个问题.

更新

@Validateable
class commandClass {
    //required fields and constraints
    def formatData(List<commandClass> commandObjs) {
        StringBuilder validationErrors
        commandObjs.each {commandObj->
            validationErrors = new StringBuilder()
            if(commandObj.hasErrors()) {
                commandObj.errors.allErrors.each {it -> …
Run Code Online (Sandbox Code Playgroud)

java grails spock

6
推荐指数
1
解决办法
1962
查看次数